我有以下数据集:
${JELLY_SCRIPT,template="html"}
和另一个具有两列的数据集
player_ref first_name
1 a
2 b
3 c
我想替换uid shortname
1 d
2 e
3 f
处的值。我要简称,而不是player_ref == uid
。看起来像这样:
first_name
最简单的方法是什么?
答案 0 :(得分:1)
假设您的player_ref
是DataFrame索引,则可以在您的DataFrames上使用join,drop和rename:
import pandas as pd
d1 = pd.DataFrame({
'first_name': ['a','b','c','d']
}, index=[1,2,3,4])
d2 = pd.DataFrame({
'shortname': ['e','f','g','h']
}, index=[4,2,1,3])
result = d1.join(d2, how='inner').drop('first_name', axis=1).rename({'shortname': 'first_name'}, axis=1)
答案 1 :(得分:0)
您可以尝试(如果player_ref == uid):
df_player.first_name = df_uid.shortname
或if player_ref!= uid(带有示例):
df_player = pd.DataFrame({'player_ref': [1, 2 ,3, 4], 'first_name': ['a', 'b', 'c', 'd']})
df_uid = pd.DataFrame({'uid': [4, 2, 1, 3], 'shortname': ['e','f','g', 'h']})
df_player.first_name = [df_uid[df_uid.uid == p].shortname.values[0] for p in df_player.player_ref]
df_player
或使用地图:
df_player.first_name = df_player.player_ref.map(dict(e for e in zip(df_uid.uid, df_uid.shortname)))