我有以下数据帧:
df=pd.DataFrame({'state':['AL','WI','FL','NJ','BM'],'country':['USA','USA','','','']})
如果相应的州行位于州列表下,我尝试将我的国家/地区列填写为“USA”:
^{pr2}$我审阅了以下相关的SO帖子: Python Dataframe filling NaN values using information from other columns
虽然问题类似,但我无法对我的案例使用apply函数,因为我不知道如何检查值列表中是否有其他列值。我尝试了以下代码(不成功):
df['country'] = values.where(df['country'] == np.nan and df['state'] in states, others=df['country'])
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
假设空格是} 来获得更快的结果:
np.nan
,如果不是,可以用df=df.replace('',np.nan)
替换,可以使用^{相关问题 更多 >
编程相关推荐