Use DataFrame.pivot
without values parameter and then flatten 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])
尝试使用它,这将满足您的要求。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.