[英]How can I pivot a table and get the percentage of each row in Python?
[英]How can i pivot this table to get it in this format?
使用DataFrame.pivot
没有值的参数,然后压平MultiIndex
:
df1 = df.pivot(index='company', columns='year_active')
#alternative
#df1 = df1.set_index(['company','year_active']).unstack()
df1.columns = df1.columns.map(lambda x: f'{x[0]}_{x[1]}')
df.pivot(index='company', columns='year_active', values=['spend', 'counts', 'distinct_counts])
尝试使用它,这将满足您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.