请考虑以下数据框<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中获得它?谢谢。
答案 0 :(得分:1)
您可以将元组解压缩到变量a
和b
并传递给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)