python-2.7 - Pandas groupby、pivot 或 stack?将单列的组转换为多列

标签 python-2.7 pandas

我的数据如下所示:

2     PresentationID   12954
5          Attendees      65
6          Downloads       0
7          Questions       0
8              Likes      11
9             Tweets       0
10             Polls       0
73    PresentationID   12953
76         Attendees      64
77         Downloads      31
78         Questions       0
79             Likes      11
80            Tweets       0
81             Polls       0
143   PresentationID   12951
146        Attendees      64
147        Downloads      28
148        Questions       2
149            Likes       2
150           Tweets       0
151            Polls       0

我需要把它变成这种格式:
   PresentationID  Attendees  Downloads  Questions  Likes  Tweets  Polls   
0           12954         65          0          0     11       0      0   
1           12953         64         31          6       0      4   
2           12892        204          0          0     14       0      0  

我尝试了 groupby、pivot 和 stack 的几种组合,但都无济于事。非常感谢任何建议。谢谢。

最佳答案

您可以使用 cumcount pivot :

print (df)
      A               B      C
0     2  PresentationID  12954
1     5       Attendees     65
2     6       Downloads      0
3     7       Questions      0
4     8           Likes     11
5     9          Tweets      0
6    10           Polls      0
7    73  PresentationID  12953
8    76       Attendees     64
9    77       Downloads     31
10   78       Questions      0
11   79           Likes     11
12   80          Tweets      0
13   81           Polls      0
14  143  PresentationID  12951
15  146       Attendees     64
16  147       Downloads     28
17  148       Questions      2
18  149           Likes      2
19  150          Tweets      0
20  151           Polls      0

df['G'] = df.groupby('B').cumcount()
df = df.pivot(index='G', columns='B', values='C')
print (df)
B  Attendees  Downloads  Likes  Polls  PresentationID  Questions  Tweets
G                                                                       
0         65          0     11      0           12954          0       0
1         64         31     11      0           12953          0       0
2         64         28      2      0           12951          2       0
df = pd.pivot(index=df.groupby('B').cumcount(), columns=df.B, values=df.C)
print (df)
B  Attendees  Downloads  Likes  Polls  PresentationID  Questions  Tweets
0         65          0     11      0           12954          0       0
1         64         31     11      0           12953          0       0
2         64         28      2      0           12951          2       0

关于python-2.7 - Pandas groupby、pivot 或 stack?将单列的组转换为多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40382207/

相关文章:

python - 如何处理 Pandas 中的 2 列并使用新列名称创建新数据框

python - 重新分配 Pandas DataFrame 中的最大值

Python在开头和结尾加入字符

python-2.7 - 如何在TextBox中选择文本

python - Tkinter 索引词问题

python - 如何在不填充日期时间间隙的情况下进行上采样

python: object() 不带参数错误

Python - 用于选择性内存记录的临时记录器

python , Pandas : how to remove greater than sign

python - 将字符串的 Pandas DataFrame 转换为直方图