python - Pandas groupby 链接:将多索引列重命名为一行
问题描述
我正在对 Pandas 数据框进行一些连续操作,我需要链接重命名操作。情况是这样的:
import numpy as np
import pandas as pd
import seaborn as sns
df = sns.load_dataset('tips')
g = (df.groupby(['sex','time','smoker'])
.agg({'tip': ['count','sum'],
'total_bill': ['count','mean']})
.reset_index()
)
print(g.head())
这给出了:
sex time smoker tip total_bill
count sum count mean
0 Male Lunch Yes 13 36.28 13 17.374615
1 Male Lunch No 20 58.83 20 18.486500
2 Male Dinner Yes 47 146.79 47 23.642553
3 Male Dinner No 77 243.17 77 20.130130
4 Female Lunch Yes 10 28.91 10 17.431000
没有链接
我可以在另一行手动完成:
g.columns = [i[0] + '_' + i[1] if i[1] else i[0]
for i in g.columns.ravel()]
它工作正常,但我想链接这个重命名列过程,以便我可以链接进一步的其他操作。
但我想要内链
怎么做?
所需输出:
g = (df.groupby(['sex','time','smoker'])
.agg({'tip': ['count','sum'],
'total_bill': ['count','mean']})
.reset_index()
.rename(something here)
# or .set_axis(something here)
# or, .pipe(something here) I am not sure.
) # If i could do this this, i can do further chaining
sex time smoker tip_count tip_sum total_bill_count total_bill_mean
0 Male Lunch Yes 13 36.28 13 17.374615
1 Male Lunch No 20 58.83 20 18.486500
2 Male Dinner Yes 47 146.79 47 23.642553
3 Male Dinner No 77 243.17 77 20.130130
4 Female Lunch Yes 10 28.91 10 17.431000
解决方案
你可以pipe
用来处理这个:
import numpy as np
import pandas as pd
import seaborn as sns
df = sns.load_dataset('tips')
g = (df.groupby(['sex','time','smoker'])
.agg({'tip': ['count','sum'],
'total_bill': ['count','mean']})
.reset_index()
.pipe(lambda x: x.set_axis([f'{a}_{b}' if b == '' else f'{a}' for a,b in x.columns], axis=1, inplace=False))
)
print(g.head())
输出:
sex time smoker tip_count tip_sum total_bill_count total_bill_mean
0 Male Lunch Yes 13 36.28 13 17.374615
1 Male Lunch No 20 58.83 20 18.486500
2 Male Dinner Yes 47 146.79 47 23.642553
3 Male Dinner No 77 243.17 77 20.130130
4 Female Lunch Yes 10 28.91 10 17.431000
注意我使用的是 f 字符串格式,需要 python 3.6+。
推荐阅读
- python - UnicodeDecodeError:“charmap”编解码器无法解码字节 0x81
- netflix-eureka - 使用zuul、eureka、feign时如何减少延迟
- php - PHP(Laravel)Oracle连接不返回所有行
- r - R中的Quanteda tf-idf变换函数
- javascript - IE 11 用新的 HTML 页面替换当前 DOM
- python - 线程和进程编程
- java - Firebase 搜索功能
- reactjs - 如何减小 google-maps-react 中标记的自定义图标的大小?
- php - 具有相同键值的多个数组合并为一个数组
- node.js - 如何将日期和天数添加到当前日期