我有一个noob问题:
sheetlist=['SITC0_Food', 'SITC1_BevTobacco', 'SITC2_Crude', 'SITC3_MineralFuels', 'SITC4_AnimalVegOils', 'SITC5_Chemicals', 'SITC6_Manuf', 'SITC7_Machinery', 'SITC8_Misc', 'SITC9_Commodities', 'All']
for sheet in sheetlist:
sheet= pd.read_excel('ExportsMatrix.xlsx', sheetname=sheet, skiprows=3)
sheet.drop(sheet.index[0], inplace=True)
我想将每个工作表另存为一个单独的数据框,以便SITC0_Food,SITC1_BevTobacco等等是一个df。上面的代码将每个工作表保存在工作表中作为“工作表”。并在每次迭代时覆盖它。
我做错了什么?
答案 0 :(得分:1)
您可append
列出dfs
每个sheet
:
dfs = []
for sheet in sheetlist:
sheet= pd.read_excel('ExportsMatrix.xlsx', sheetname=sheet, skiprows=3)
sheet.drop(sheet.index[0], inplace=True)
dfs.append(sheet)
print (dfs[0])
另一种解决方案是使用dict
来存储DataFrames
:
dfs = {}
for sheet in sheetlist:
df = pd.read_excel('ExportsMatrix.xlsx', sheetname=sheet, skiprows=3)
df.drop(df.index[0], inplace=True)
dfs[sheet] = df
print (dfs['SITC0_Food'])