skdaima

这篇博客介绍了如何用Python处理缺失值,通过计算每列的众数填充NaN,利用pivot_table进行分组平均填补,根据特定列的组合值进行填充,并进行数据分箱和编码,最后使用get_dummies进行one-hot编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

data[‘Gender’].fillna(data[‘Gender’].mode().iloc[0],inplace=True)
data[‘Married’].fillna(data[‘Married’].mode().iloc[0],inplace=True)
data[‘Self_Employed’].fillna(data[‘Self_Employed’].mode().iloc[0],inplace=True)

data[‘Gender’].fillna(data[‘Gender’].iloc[0])

impute_grps = data.pivot_table(values=[“LoanAmount”],index=[“Gender”,“Married”,“Self_Employed”],aggfunc=np.mean)
for i,row in data.loc[data[‘LoanAmount’].isnull(),:].iterrows():
ind = tuple ([row[‘Gender’],row[‘Married’],row[‘Self_Employed’]])
data.loc[i,‘LoanAmount’] = impute_grps.loc[ind].values[0]

data_merged = data.merge(right=prop_rates,how=‘inner’,left_on=‘Property_Area’,right_index=True,sort=False)
data_merged.pivot_table(values=‘Credit_History’,index=[‘Property_Area’,‘rates’],aggfunc=len)

cut_points = [90,140,190]
break_points = [data[‘LoanAmount’].min()] + cut_points + [data[‘LoanAmount’].max()]
print(break_points)
labels = [“low”,“medium”,“high”,“very_high”]
data[‘LoanAmount_Bin’] = pd.cut(data[‘LoanAmount’],bins=break_points,right=False,labels=labels,include_lowest=True)
pd.value_counts(data[‘LoanAmount_Bin’],sort=False)

data[‘Loan_Status_Coded’] = data[“Loan_Status”].replace({‘N’:0,‘Y’:1})
data[‘Loan_Status_Coded’].value_counts()

dummies = pd.get_dummies(data[‘LoanAmount_Bin’],prefix=‘LoanAmount’)

data_onehot = pd.concat([data,dummies],axis=1)
data_onehot.head(10)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值