在循环中将多个熊猫数据框导出到多个Excel文件

时间:2018-12-16 15:40:08

标签: python pandas loops dataframe

import pandas as pd

mydata={'consum_amount_rice':[21,21,4,8,7,7,28,42,21,7], 
        'consum_unit_rice':[1,1,2,1,6,6,5,5,5,6]}

df = pd.DataFrame(data=mydata)

units=[1,2,5,6]

for x in units:
    dfx = df[df['consum_unit_rice'] ==x]

我想将多个数据框dfx导出到多个Excel文件,例如:

file1 = 'G:\python\out\consumption_rice_1.xlsx',用于df1
file2 = 'G:\python\out\consumption_rice_2.xlsx',代表df2
file5 = 'G:\python\out\consumption_rice_5.xlsx',代表df5
file6 = 'G:\python\out\consumption_rice_6.xlsx',代表df6

我可以在此循环中使用pd.to_excel吗?

0 个答案:

没有答案