我有一个像下面这样的dict,它代表一场赛马。数据集中有许多比赛,按raceId分组:
data_orig = {
'meetingId': [178515] * 6,
'raceId': [879507] * 6,
'horseId': [90001, 90002, 90003, 90004, 90005, 90006],
'position': [1, 2, 3, 4, 5, 6],
'weight': [51, 52, 53, 54, 55, 56],
}
字符串
我想把每一行的马的具体数据添加到每一行。结果应该是这样的:
data_new = {
'meetingId': [178515] * 6,
'raceId': [879507] * 6,
'horseId_a':[90001, 90002, 90003, 90004, 90005, 90006],
'position_a':[1, 2, 3, 4, 5, 6],
'weight_a':[51, 52, 53, 54, 55, 56],
'horseId_b':[90002, 90003, 90004, 90005, 90006, 90001],
'position_b':[2, 3, 4, 5, 6, 1],
'weight_b':[52, 53, 54, 55, 56, 51],
'horseId_c':[90003, 90004, 90005, 90006, 90001, 90002],
'position_c':[3, 4, 5, 6, 1, 2],
'weight_c':[53, 54, 55, 56, 51, 52],
'horseId_d':[90004, 90005, 90006, 90001, 90002, 90003],
'position_d':[4, 5, 6, 1, 2, 3],
'weight_d':[54, 55, 56, 51, 52, 53],
'horseId_e':[90005, 90006, 90001, 90002, 90003, 90004],
'position_e':[5, 6, 1, 2, 3, 4],
'weight_e':[55, 56, 51, 52, 53, 54,],
'horseId_f':[90006, 90001, 90002, 90003, 90004, 90005],
'position_f':[6, 1, 2, 3, 4, 5],
'weight_f':[56, 51, 52, 53, 54, 55],
}
型
我在下面试过了,这是对矩阵的调换。
data_orig_df = pd.DataFrame(data_orig)
new_df = pd.DataFrame()
for index, row_i in data_orig_df.iterrows():
horseId = row_i['horseId']
row_new = row_i.copy()
for index, row_j in race_df.iterrows():
if row_j['horseId']:
continue
row_new = pd.merge(row_new, row_j[getHorseSpecificCols()], suffixes=('', row_j['position']))
new_df = pd.concat([new_df, row_new], axis=1)
型
谢谢你的帮忙。
1条答案
按热度按时间8qgya5xd1#
您可以使用numpy轻松地滚动/索引值:
字符串
输出量:
型