我有一个数据帧
game_id move_number move colour avg_centi phase
0 03gDhPWr 1 e4 white NaN opening
1 03gDhPWr 2 d5 black 37.0 opening
2 03gDhPWr 3 e5 white 61.0 opening
3 03gDhPWr 4 c5 black -5.0 opening
4 03gDhPWr 5 Nf3 white 26.0 opening
... ... ... ... ... ... ...
110093 zzaiRa7s 36 a5+ black NaN endgame
110094 zzaiRa7s 37 Kxb5 white NaN endgame
110095 zzaiRa7s 38 c6+ black NaN endgame
110096 zzaiRa7s 39 Ka4 white NaN endgame
110097 zzaiRa7s 40 Q@b4# black NaN endgame
我想映射colour
列,这样当颜色为black
时,colour
列的值将替换为phase
列中的值。我希望使用pandas map
或apply
函数来实现这一理想。不是replace
太慢了。在
生成的数据帧应如下所示:
^{pr2}$我尝试过以下代码,但似乎不太管用:
def wrangle_game_phase(x):
if x == 'black':
return phase
else:
return x
df['game_type'] = df['colour'].apply(wrangle_game_phase)
你试过了吗?loc…请试一下:
df.loc[df['Color'] =='Black', 'Color'] = df['Phase']
对
pd.DataFrame.loc
使用布尔索引:使用
np.where
例如::
相关问题 更多 >
编程相关推荐