如何使用fillna()函数在python中填写分类变量的NA / Null

问题描述 投票:0回答:1

我有一个包含一些分类变量的数据集,它们有一些缺失(NA / Null)。我想用该列的模式填充这些NA / Null。我累了以下事情,但这没用

MD=Data['Gender'].mode()
Data['Gender'].fillna(value=MD,inplace=True)


MD=Data['Married'].mode()
Data['Married'].fillna(value=MD,inplace=True)

MD=Data['Dependents'].mode()
Data['Dependents'].fillna(value=MD,inplace=True)

MD=Data['Self_Employed'].mode()
Data['Self_Employed'].fillna(value=MD,inplace=True)

MD=Data['Credit_History'].mode()
Data['Credit_History'].fillna(value=MD,inplace=True)

Gender                26
Married                6
Dependents            30
Education              0
Self_Employed         64
ApplicantIncome        0
CoapplicantIncome      0
LoanAmount             0
Loan_Amount_Term       0
Credit_History       100
Property_Area          0
Loan_Status            0

仍然显示缺失值。

python pandas numpy machine-learning
1个回答
1
投票

试试这个:

Data['Married'].fillna(Data['Married'].mode(), inplace=True)

或试试这个:

Data['Married'].fillna(Data['Married'].value_counts().index[0], inplace=True)

确保分类变量的dtype是对象或类别。

© www.soinside.com 2019 - 2024. All rights reserved.