我正试图重塑我的数据。我有战斗和一排数据,每一个在给定战斗中战斗的战士。我想从每一场战斗中与对手取得第二行,并将该行中的值转换为列。我已经成功地将我的初始数据集从长到宽,但这最后一步很艰难
以下是我的数据示例:
{'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()
您需要按^{} 按计数器创建} 重新整形,并按
MultIindex
,按^{map
最后展平列中的MultiIndex
:相关问题 更多 >
编程相关推荐