尝试使用Python从excel电子表格中删除顶行

2024-09-28 17:05:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在这里的第一个问题,所以如果我没有遵守完美的礼仪,请原谅我。我已经找了6个小时的答案,所以我想是时候问了。我是Python的新手,我正在尝试使用Selenium自动化数据输入。我做得很好,但我仍然坚持从Excel中提取数据。数据拉取不是问题,但告诉我的excel删除最上面的一行,或者移到下一行。以下是我的脚本示例:

    import pandas as pd
from pandas import ExcelWriter
import numpy as np

xl = pd.ExcelFile('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')
xl

wr = pd.ExcelWriter('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')

xl.sheet_names

['Sheet1']

df = xl.parse('Sheet1') #This reads the sheet into a dataframe
df

cp = pd.read_excel('C:\\Users\\Steph_000\\Desktop\\students2.xlsx')

IDNum = cp[:1] #This pulls the first row as a variable

print(IDNum) #This is where it prints to the search bar to look the student up

chart = cp[1:] #This pulls everything but the first row as a variable

print(chart)

df.to_excel(wr, 'Sheet1')
wr.save() #This is supposed to overwrite it, but just saves it as blank

不管我做什么,我都不能让它往上移动,或者诸如此类的事情。我真的很挣扎,也很沮丧。如果必要的话,我将使用与Pandas不同的Python包装器,但我已经尝试过openpyxl、xlrd和xlwr,但都没有用!把我的头发拔出来,先谢谢你!在


Tags: theto数据importasthisxlsxwr
1条回答
网友
1楼 · 发布于 2024-09-28 17:05:50

请看下面的示例:

  • 缺少引擎=“xlsxwriter”
  • 缺少sheet_name='Sheet1

看看这个:

            import pandas as pd
            #Create a Pandas dataframe from the data.
            df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

            # Create a Pandas Excel writer using XlsxWriter as the engine.
            writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

            # Convert the dataframe to an XlsxWriter Excel object.
            df.to_excel(writer, sheet_name='Sheet1')

            # Close the Pandas Excel writer and output the Excel file.
            writer.save()

http://xlsxwriter.readthedocs.io/working_with_pandas.html

相关问题 更多 >