我想编辑同一 Excel 文件中存在的多个工作表,然后保存所做的调整。这些工作表具有相同的列标题,称为贷方和借方。我创建的代码如下:import pandas as pdimport numpy as npclass blah: def __init__(self, path, file_in, file_out): self.path = path self.file_inviato = file_in self.file_out = file_out def process_file(self): df = pd.read_excel(self.path + self.file_in, sheet_name=None, skiprows=4) ****Here is where I am struggling in amending both worksheets at the same time**** # df = df.columns.str.strip() # df['Col1'] = np.where((df['Col2'] == 'KO') | (df['Col2'] == 'OK'), 0, df['Col1']) writer = pd.ExcelWriter(self.path + self.file_out, engine='xlsxwriter') for sheet_name in df.keys(): df[sheet_name].to_excel(writer, sheet_name=sheet_name, index=False) writer.save() b = blah('path....', 'file in....xlsx', 'file out.xlsx')b.process_file()
1 回答
侃侃无极
TA贡献2051条经验 获得超10个赞
找到了解决方法:
for sheet_name in df.keys():
df[sheet_name] = df[sheet_name].rename(columns=lambda x: x.strip())
df[sheet_name]['Col1'] = np.where((df[sheet_name]['Col2'] == 'KO') |
(df[sheet_name]['Col2'] == 'OK'), 0, df[sheet_name]['Col1'])
添加回答
举报
0/150
提交
取消