无法用所有列的列值填充缺失值
Unable to fill missing values with column value across all columns
我有一个如下所示的数据框
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]})
我的 objective 是在所有行和列中用相应的 credit_history
值填充缺失值
我尝试了以下方法,但它不起作用
cols = ['Loan_Status','subject_status','test_status']
df[cols] = df[cols].fillna(df['Credit_History'])
我希望我的输出如下所示。基本上,无论哪一行丢失,它都应该从 credit_history
列
中选择相应的值
使用DataFrame.apply
, so is used 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
我有一个如下所示的数据框
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]})
我的 objective 是在所有行和列中用相应的 credit_history
值填充缺失值
我尝试了以下方法,但它不起作用
cols = ['Loan_Status','subject_status','test_status']
df[cols] = df[cols].fillna(df['Credit_History'])
我希望我的输出如下所示。基本上,无论哪一行丢失,它都应该从 credit_history
列
使用DataFrame.apply
, so is used 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