简体   繁体   English

根据条件 dataframe 填充 NaN 值 python pandas

[英]Filling the NaN values in the column dataframe based on condition python pandas

I am trying to update my dataframe column value based on the condition but when i check my data frame its value is not getting updated.我正在尝试根据条件更新我的 dataframe 列值,但是当我检查我的数据框时,它的值没有得到更新。

for i in titanic['Survived'].unique():
meanAge = titanic.Age[titanic['Survived'] == i].mean()
meanAge = "{:.1f}".format(meanAge)
df = titanic['Survived'] == i
df1 = titanic.Age[df];
df1.fillna(meanAge, inplace=True)
#print (df1) the value seems to be updated here

but print(titanic still shows NAN values.但是 print(titanic 仍然显示 NAN 值。

The reason is that most likely df1 is a copied object from the dataframe, and it does not reference the titanic dataframe.原因是df1很可能是从 dataframe 复制的 object,它没有引用titanic dataframe。

It will probably help you to do like below (just replace the condition with one that includes NaN values).它可能会帮助您像下面那样做(只需将条件替换为包含 NaN 值的条件)。 So instead of calling the method fillna just use the assignment operator with the proper index.因此,不要调用方法fillna ,只需使用具有适当索引的赋值运算符。

titanic.Age[titanic['Survived'] == i] = meanAge

If you don't have memory constraints is better to think about dataframes as immutable.如果您没有 memory 约束,则最好将数据帧视为不可变的。

Instead of updating in place, try to make a copy of the original and update the new one.不要就地更新,而是尝试制作原始副本并更新新副本。

With the exception of shallow copy operations, and operations with "inplace" as an argument, all operations generate a copy .除了浅拷贝操作和以“inplace”为参数的操作外,所有操作都会生成一个副本

You can directly update the titanic DataFrame after:之后可以直接更新titanic DataFrame:

for i in titanic['Survived'].unique():
    meanAge = titanic.Age[titanic['Survived'] == i].mean()
    meanAge = "{:.1f}".format(meanAge)
    df = titanic['Survived'] == i
    df1 = titanic.Age[df];
    df1.fillna(meanAge, inplace=True)
    titanic.loc[df, 'Age'] = df1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM