python - 即使使用 inplace=True,pandas replace 也不会替换值

标签 python pandas

我的数据是这样的。如果 'no_of_children' 不是 nan,我想用 'Married' 替换 marital_status 'Missing'

>cust_data_df[['marital_status','no_of_children']]
>

    marital_status  no_of_children

0   Married           NaN
1   Married           NaN
2   Missing            1
3   Missing            2
4   Single            NaN
5   Single            NaN
6   Married           NaN
7   Single            NaN
8   Married           NaN
9   Married           NaN
10  Single            NaN

这是我尝试过的:

cust_data_df.loc[cust_data_df['no_of_children'].notna()==True, 'marital_status'].replace({'Missing':'Married'},inplace=True)

但这并没有做任何事情。

最佳答案

为避免 chained assignments 分配回替换值:

m = cust_data_df['no_of_children'].notna()
d = {'Missing':'Married'}
cust_data_df.loc[m, 'marital_status'] = cust_data_df.loc[m, 'marital_status'].replace(d)

如果需要设置所有值:

cust_data_df.loc[m, 'marital_status'] = 'Married'

编辑:

感谢@Quickbeam2k1 的解释:

cust_data_df.loc[cust_data_df['no_of_children'].notna()==True, 'marital_status'] is just a new object which has no reference. Replacing there, will leave the original object unchanged

关于python - 即使使用 inplace=True,pandas replace 也不会替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152897/

相关文章:

Python try/except 不断尝试直到没有错误

pandas - 有没有一种简单的方法可以对 Pandas DataFrame 中的列进行分组?

Python。从 data.frame 获取结构

python - 使用带有 key 的 github

python - 将我的 python 脚本添加到系统托盘?或开始菜单,运行 mu 简单的 GUI?

python - 使用pivot_table时将分类数据与数值数据相结合

python - 日志消息在控制台 Python 中出现两次

python - 无法创建具有特定数量的类标签的 Pandas 数据框

python - 使用 _leftonly 或 _rightonly 中的值填充 2 列上合并的 Df 外部的缺失值

python - 为什么 df.where() 不替换所有空值?