python - 有条件 reshape pandas

标签 python pandas

我正在尝试 reshape 我的数据。我有战斗和一排包含在给定战斗中战斗的每个战士的数据。我想从每次战斗中获取对手的第二行,并将该行中的值转换为列。我已经成功地将我的初始数据集从长数据集转向宽数据集,但在这最后一步中遇到了困难。

这是我的数据示例:

{'event_id': {0: '417', 1: '417', 2: '56', 3: '56'},
 'fighter': {0: 'PRICE', 1: 'RABOTTE', 2: 'PRICE', 3: 'WILDER'},
 'punches_landed|Jabs': {0: 51, 1: 25, 2: 1, 3: 12},
 'punches_landed|Power Punches': {0: 10, 1: 11, 2: 19, 3: 16},
 'punches_landed|Total Punches': {0: 61, 1: 36, 2: 20, 3: 28},
 'punches_thrown|Jabs': {0: 271, 1: 94, 2: 86, 3: 49},
 'punches_thrown|Power Punches': {0: 29, 1: 47, 2: 41, 3: 23},
 'punches_thrown|Total Punches': {0: 300, 1: 141, 2: 127, 3: 72}}

所需的输出类似于此

 event_id fighter puncheslanded... punches_throwns... fighter2 puncheslanded2....punches_thrown2
   417     PRICE    ...                   ...         RABOTTE                                        
    56     PRICE    ...                   ...         WILDER

这就是我到目前为止所做的

#this pivoted the original dataset
fight_stats = fight_stats.pivot_table(['punches_landed','punches_thrown'],['event_id','fighter'],'punch_stat').reset_index()

最佳答案

您需要通过GroupBy.cumcount计数器创建MultIindex , reshape DataFrame.unstack最后通过 map 压平列中的 MultiIndex:

df = (df.set_index(['event_id',df.groupby('event_id').cumcount().add(1)])
        .unstack()
        .sort_index(axis=1, level=1))
df.columns = df.columns.map('{0[0]}{0[1]}'.format)
df = df.reset_index()
print (df)
  event_id fighter1  punches_landed|Jabs1  punches_landed|Power Punches1  \
0      417    PRICE                    51                             10   
1       56    PRICE                     1                             19   

   punches_landed|Total Punches1  punches_thrown|Jabs1  \
0                             61                   271   
1                             20                    86   

   punches_thrown|Power Punches1  punches_thrown|Total Punches1 fighter2  \
0                             29                            300  RABOTTE   
1                             41                            127   WILDER   

   punches_landed|Jabs2  punches_landed|Power Punches2  \
0                    25                             11   
1                    12                             16   

   punches_landed|Total Punches2  punches_thrown|Jabs2  \
0                             36                    94   
1                             28                    49   

   punches_thrown|Power Punches2  punches_thrown|Total Punches2  
0                             47                            141  
1                             23                             72  

关于python - 有条件 reshape pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59209699/

相关文章:

python - 将 float 分配给子集返回 1

python - scikit-learn 的 LabelEncoder() 内存问题

python - turtle 颜色不随变量和列表而改变

python - pip 正在尝试将带有 C 扩展名的包安装到 OSX 上的全局目录中

c# - Python.NET : "Cannot instantiate enumeration"

python - 如何在 Python 中转换指数并摆脱 'e+' ?

python - Pandas 将变量添加为列并对应于原始表(但它们的长度不同)

python - 将随机列乘以另一列

python - 收集日志选项卡的输出(Chromium 开发者工具)

python - 值错误: matrix must be 2-dimensional when passing two arrays to the function