使用字典值或键的Python fillna
我有一个dataframe,我试图填充一个现有列,但会满足于一个新列使用字典值或键的Python fillna,python,pandas,Python,Pandas,我有一个dataframe,我试图填充一个现有列,但会满足于一个新列 The code i have tried is(which does not work): df1['NewCol1'] = df1['ID'].map(dict1.get(df1['Name'])) #but the code i would like to use is: df1['ExistingColumn'] = df1['ID'].ifna().map(dict1.get(df1['Name'])) #i a
The code i have tried is(which does not work):
df1['NewCol1'] = df1['ID'].map(dict1.get(df1['Name'])) #but the code i would like to use is:
df1['ExistingColumn'] = df1['ID'].ifna().map(dict1.get(df1['Name'])) #i am trying to fill an existing column
#the dictionary i am using is:
dict1 = {'Name':'ID'}
我正在使用的数据帧如下所示:
ID Name ExistingColumn
1 name1 id1
1 name2 id1
2 name3 id2
NA name3 id2
NA name2 id1
另外,如果可以做到这一点,有没有办法通过按值引用字典并拉回键来做到这一点 我们有
fillna
df.ID.fillna(df.Name.map(dict),inplace=True)
你能提供1)你的字典2)你的输出数据框的例子吗。请提供一个。非常感谢!有趣的是,我不必将其设置为特定列,只需运行您提供的语句即可!这太棒了,我真的很感谢你的帮助,因为我已经围绕这个答案跳舞了5个小时