gpt4 book ai didi

python - 无法用所有列的列值填充缺失值

转载 作者:行者123 更新时间:2023-12-04 14:03:08 25 4
gpt4 key购买 nike

我有一个如下所示的数据框

df = pd.DataFrame({'Credit_History':['Yes','ABC','DEF', 'JKL'],
'Loan_Status':['T1','T2',np.nan,np.nan],
'subject_status':['DUMMA','CHUMMA',np.nan,np.nan],
'test_status':['test',np.nan,np.nan,np.nan]})

我的目标是用所有行和列中相应的 credit_history 值填充缺失值

下面的方法我都试过了,还是不行

cols = ['Loan_Status','subject_status','test_status']
df[cols] = df[cols].fillna(df['Credit_History'])

我希望我的输出如下所示。基本上,无论哪一行缺失,它都应该从 credit_history

中选择相应的值

enter image description here

最佳答案

使用DataFrame.apply ,所以用了Series.fillna :

cols = ['Loan_Status','subject_status','test_status']
df[cols] = df[cols].apply(lambda x: x.fillna(df['Credit_History']))

print (df)
Credit_History Loan_Status subject_status test_status
0 Yes T1 DUMMA test
1 ABC T2 CHUMMA ABC
2 DEF DEF DEF DEF
3 JKL JKL JKL JKL

或转置:

cols = ['Loan_Status','subject_status','test_status']
df[cols] = df[cols].T.fillna(df['Credit_History']).T

print (df)
Credit_History Loan_Status subject_status test_status
0 Yes T1 DUMMA test
1 ABC T2 CHUMMA ABC
2 DEF DEF DEF DEF
3 JKL JKL JKL JKL

关于python - 无法用所有列的列值填充缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69358724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com