我是编码新手,我有2个数据帧,我将发布如下:
原始数据:
country_code homicides_per_100k
ABC 2.6
ABB nan
ACC nan
霍米尤集:
Country Code year
ABC 2.6
ACC 11
ABB 3.1
ADD 0.5
两个数据帧的顺序和形状不同
如何使用homi_集合中的数据替换原始_数据中的nan
我的代码看起来像这样。它不起作用:
for row, homicide in enumerate(raw_data['homicides_per_100k']):
if homicide == "":
country_code = raw_data.loc[row, 'country_code']
homi_set_index = homi_set.index[homi_set['Country Code'] == country_code]
homi_value = homi_set.loc[homi_set_index, '2014']
raw_data.loc[row, 'homicides_per_100k'] = homi_value
set_index
+combine_first
。设置索引使其基于country_code
更新值。如果raw_data
中有不同的值,则Combine first会优先考虑raw_data
中的非空值相关问题 更多 >
编程相关推荐