How split a pandas dataframe in muliple excel files -
i have data frame sales related different countries. need crate different excel each of country have in dataframe. not sure how can accomplish in pandas
consider dataframe df
df = pd.dataframe(dict( sales=[1, 2, 3, 4], country=['jp', 'cn', 'uk', 'au'] )) print(df) country sales 0 jp 1 1 cn 2 2 uk 3 3 au 4
we can iterate through groupby
object , use to_excel
for n, g in df.groupby('country'): # `n` group name, country g.to_excel('{}.xlsx'.format(n))
this have created files
['au.xlsx', 'cn.xlsx', 'jp.xlsx', 'uk.xlsx']
Comments
Post a Comment