我有一个像这样的df:
df=pd.DataFrame({'A':[np.nan, np.nan, 'w', np.nan, 'y', np.nan],
'B': [np.nan, 'G', 'G', np.nan, 'R', 'R' ]})
# And I zipped the unique values of the two groups together
univalue=zip(df.A.dropna().unique().tolist(), df.B.dropna().unique().tolist())
我想给A列赋值,如果B中的值不是nan,则A列具有zip列表中的坐标值。所以df就像下面这样。有什么办法吗?
> df
A B
nan nan
'w' 'G'
'w' 'G'
nan nan
'y' 'R'
'y' 'R'
如果我理解正确的话,您可以创建一个映射字典,然后使用.map
分配给列" a ";
univalue = dict(zip(df.B.dropna().unique(), df.A.dropna().unique()))
df["A"] = df["B"].map(univalue)
print(df)
打印:
A B
0 NaN NaN
1 w G
2 w G
3 NaN NaN
4 y R
5 y R