I'm using famous Titanic dataset for my first Kaggle problem. I'm getting stuck in dataset. I want to replace NaN values of Age gender wise e.g. missing values for 'male' should get replaced by average age of Male and vice-versea. While my code is running fine but getting an exception as following: "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._update_inplace(new_data)"
我正在使用着名的泰坦尼克号数据集来解决我的第一个Kaggle问题。我陷入了数据集。我想要替换Age性别的NaN值,例如“男性”的缺失值应该被男性和反对的平均年龄所取代。虽然我的代码运行正常但得到如下异常:“SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值请参阅文档中的警告:http://pandas.pydata.org/pandas -docs / stable / indexing.html #indexing-view-versus-copy self._update_inplace(new_data)“
import pandas as pd
df=pd.read_csv('train.csv')
df[(df['Sex']=='male') & (df['Age'].apply(np.isnan))]['Age'].fillna(df[df['Sex']=='male']['Age'].mean(),inplace=True)
1 个解决方案
#1
1
import pandas as pd
import numpy as np
df = pd.read_csv('train.csv')
df['Age'].fillna(df.groupby(["Sex"])["Age"].transform(np.mean), inplace=True)
Maybe this was something you were trying to do? I didn't get any warning though. Have a look at my blog post too if necessary.
也许这是你想要做的事情?我没有得到任何警告。如有必要,请查看我的博客文章。
#1
1
import pandas as pd
import numpy as np
df = pd.read_csv('train.csv')
df['Age'].fillna(df.groupby(["Sex"])["Age"].transform(np.mean), inplace=True)
Maybe this was something you were trying to do? I didn't get any warning though. Have a look at my blog post too if necessary.
也许这是你想要做的事情?我没有得到任何警告。如有必要,请查看我的博客文章。