【发布时间】:2016-10-10 17:07:51
【问题描述】:
我需要读取一个 excel 文件并在每张纸上执行一些计算。基本上,如果列日期不是“今天”,它需要删除行。
到目前为止我得到了这个代码:
导入日期时间 将熊猫导入为 pd
'''
Parsing main excel sheet to save transactions != today's date
'''
mainSource = pd.ExcelFile('path/to/file.xlsx')
dfs = {sheet_name: mainSource.parse(sheet_name)
for sheet_name in mainSource.sheet_names }
for i in dfs:
now = datetime.date.today();
dfs = dfs.drop(dfs.columns[6].dt.year != now, axis = 1); # It is the 6th column
if datetime.time()<datetime.time(11,0,0,0):
dfs.to_excel(r'path\to\outpt\test\'+str(i)+now+'H12.xlsx', index=False); #Save as sheetname+timestamp+textstring
else:
dfs.to_excel(r'path\to\output\'+str(i)+now+'H16.xlsx', index=False)
运行脚本时出现以下错误:
dfs = dfs.drop(...):
AttributeError: 'dict' object has no attribute 'drop'
有什么建议吗?
谢谢!
【问题讨论】:
-
我认为您需要将
dfs替换为i:i= i.drop(i.columns[6].dt.year != now, axis = 1); -
然后
i.to_excel(...) -
对,dfs 是数据帧的字典,所以不能像 .drop 那样对它使用数据帧操作。
-
感谢您的回复。但是,尝试过并收到错误消息
AttributeError: 'str' object has not attribute 'drop'
标签: python excel pandas text-parsing