简体   繁体   English

创建转置和 GroupBy 矩阵 Dataframe Python Pandas

[英]Create a Transpose and GroupBy Matrix Dataframe Python Pandas

I have the follow table with the columns names ID,STORE and PUSH available.我有下表,其中列名 ID、STORE 和 PUSH 可用。

With the PUSH values for each row(in this case I just have a ID, but the table contains more), I would like to create the second table(yellow one in the picture).使用每行的 PUSH 值(在这种情况下,我只有一个 ID,但表包含更多),我想创建第二个表(图中的黄色表)。

Then the target is: create new columns for each STORE(listed 7 adh,ayc,maeg,rot,witz,mar,bud), where each store will receive the value from the PUSH column.然后目标是:为每个商店创建新列(列出 7 adh、ayc、maeg、rot、witz、mar、bud),其中每个商店将从 PUSH 列接收值。

The result expected is the yellow table which I will add in the same dataframe used to generate the ID,STORE,PUSH tables.预期的结果是我将添加到用于生成 ID、STORE、PUSH 表的同一个 dataframe 中的黄色表。

Any help would be very appreciated !任何帮助将不胜感激!

the code I tried was:
df['ADH'] = combined_sf2.groupby('PNO') 
['Push'].transform() 
df['AYC'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['ADH'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['MAEG'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['ROT'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['WITZ'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['BUD'] = combined_sf2.groupby('PNO') 
['Push'].transform()
df['MAR'] = combined_sf2.groupby('PNO') 
['Push'].transform()

But is retuning just 1 value for all lines但是只为所有行重新调整 1 个值

在此处输入图像描述

# pivot to get the right table format (ID as index, STORE as column
# and PUSH as values).
# the second part (with the loc) is here to repeat the lines according
# to each ID.

pd.pivot(df, index='ID', columns='STORE', values='PUSH').loc[df.ID]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM