根据邮编列表分配空值 python
Assign empty value based on a zip list python
我有一个这样的 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())
如果 B 中的值不是 nan,我想给 A 列赋值,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
我有一个这样的 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())
如果 B 中的值不是 nan,我想给 A 列赋值,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