【发布时间】:2020-06-06 14:30:04
【问题描述】:
我有一个包含两列的 pandas 数据框:locationid、geo_loc。 locationid 列有缺失值。
我想获取缺少的 locationid 行的 geo_loc 值, 然后在 geo_loc 列中搜索这个 geo_loc 值并获取位置 ID。
df1 = pd.DataFrame({'locationid':[111, np.nan, 145, np.nan, 189,np.nan, 158, 145],
'geo_loc':['G12','K11','B16','G12','B22','B16', 'K11',he l 'B16']})
df
我需要这样的最终输出:
locationid 的索引 1 缺失,对应的 geo_loc 值为 'K11'。 我会在 geo_loc 列中查看这个“K11”,索引 6 的 locationid 为 158。有了这个值 我想填充索引 1 中的缺失值。
我尝试了这些代码,但它们不起作用。
df1['locationid'] = df1.locationid.fillna(df1.groupby('geo_loc')['locationid'].max())
df1['locationid'] = df1.locationid.fillna(df1.groupby('geo_loc').apply(lambda x: print(list(x.locationid)[0])))
【问题讨论】:
标签: python pandas dataframe machine-learning data-science