我有两个看起来像
的数据框 team points
0 1 2.5
1 2 3.2
2 5 5.8
3 3 2.8
4 4 1.9
和:
team1 team2
0 1 5
1 2 4
2 3 1
预期的输出应该给我一个与获胜者更多的专栏(更多分):
team1 team2 winner
1 5 5
2 4 2
3 1 3
答案 0 :(得分:3)
这是使用applymap
,df.idxmax()
和df.lookup
的方法:
s=df2.applymap(df1.set_index('team')['points'].get).idxmax(1)
或者由@ user3483203提供更好的选择
s=df2.stack().map(df1.set_index('team')['points']).unstack().idxmax(1)
#s.tolist() gives ['team2', 'team1', 'team1']
df2['winner']=df2.lookup(s.index,s)
print(df2)
team1 team2 winner
0 1 5 5
1 2 4 2
2 3 1 3
答案 1 :(得分:3)
尝试避免使用applymap
并使用lookup
+ reshape
x = df.set_index('team').lookup(df2.values.ravel('F'), ["points"]*df2.size)
.reshape(df2.shape, order='F')
.argmax(1)
df2['winner'] = df2.lookup(df2.index, df2.columns[x])
team1 team2 winner
0 1 5 5
1 2 4 2
2 3 1 3
答案 2 :(得分:0)
仅使用pandas.Series.map,DataFrame.stack和DataFrame.unstack的替代解决方案:
df_match['winner']=df_match.stack().map(df.set_index('team')['points']).unstack().max(axis=1).map(df.set_index('points')['team'])
print(df_match)
team1 team2 winner
0 1 5 5
1 2 4 2
2 3 1 3
答案 3 :(得分:0)
我的“简单”解决方案:
df3= df2.replace(df1.set_index("team").points.to_dict())
df2["winner"]= np.where(df3.team1>=df3.team2,df2.team1,df2.team2)