在 Pandas 中用 .loc 覆盖 Nan 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42449169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overwriting Nan values with .loc in Pandas
提问by ErnieandBert
I tried to solve the required task with the following code line:
我尝试使用以下代码行解决所需的任务:
df['Age'][np.isnan(df["Age"])] = rand1
df['Age'][np.isnan(df["Age"])] = rand1
But this raises a "SettingWithCopyWarning" and I think locating the Nan values in the dataframe (Column 'Age') by using the .locfeature might be a better way of doing this.
但这会引发“SettingWithCopyWarning”,我认为使用该.loc功能在数据帧(列“年龄”)中定位 Nan 值可能是更好的方法。
I already took a look at the documentation, but still don't know how I can fix this problem. Couldn't find any solutions on here with .loceither.
我已经查看了文档,但仍然不知道如何解决此问题。在这里.loc也找不到任何解决方案。
I would appreciate any hints and advice.
我将不胜感激任何提示和建议。
回答by jezrael
You need fillnafor replace NaNto some value:
您需要fillna替换NaN为某个值:
df.Age = df.Age.fillna(rand1)
Your solution with loc:
您的解决方案loc:
df.loc[np.isnan(df["Age"]), 'Age'] = rand1
#same as
#df.loc[df["Age"].isnull(), 'Age'] = rand1
You can also check indexing view versus copy.
您还可以检查索引视图与复制。
Sample:
样本:
df = pd.DataFrame({'Age':[20,23,np.nan]})
print (df)
Age
0 20.0
1 23.0
2 NaN
rand1 = 30
df.Age = df.Age.fillna(rand1)
print (df)
Age
0 20.0
1 23.0
2 30.0
#if need cast to int
df.Age = df.Age.fillna(rand1).astype(int)
print (df)
Age
0 20
1 23
2 30


