将数据框按多列拆分为多个Excel

时间:2019-05-16 07:59:39

标签: python pandas format

请考虑以下数据框<GoogleMap/>

df

我想对df = pd.DataFrame(dict( Id = [1, 2, 3, 3], Country=['jp', 'cn', 'uk', 'uk'], Sales = [5, 3, 3, 4] )) print(df) Id Country Sales 0 1 jp 5 1 2 cn 3 2 3 uk 3 3 3 uk 4 Id列进行迭代和分组,然后写入excel:

Country

它将创建文件: for n, g in df.groupby(['Id', 'Country']): print(n) # `n` is the group name, which will be the country g.to_excel('{}.xlsx'.format(n), index = False) ,但我想要这样的格式:["(1, 'jp').xlsx", "(2, 'cn').xlsx", "(3, 'uk').xlsx"]

如何在Pandas中获得它?谢谢。

2 个答案:

答案 0 :(得分:1)

您可以将元组解压缩到变量ab并传递给format函数:

for (a, b), g in df.groupby(['Id', 'Country']):
    print(a, b)
    # `n` is the group name, which will be the country
    g.to_excel('{}_{}.xlsx'.format(a, b), index = False)

或通过索引按位置选择元组:

for n, g in df.groupby(['Id', 'Country']):
    print(n)
    # `n` is the group name, which will be the country
    g.to_excel(f'{n[0]}_{n[1]}.xlsx'.format(n), index = False)

答案 1 :(得分:1)

for n, g in df.groupby(['Id', 'Country']):
    print(n)
    g.to_excel('{}.xlsx'.format("_".join([str(x) for x in n]), index = False)