Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill missing values of one column from another column in pandas

I have two columns in my pandas dataframe.

I want to fill the missing values of Credit_History column (dtype : int64) with values of Loan_Status column (dtype : int64).

like image 654
Rohit Patwa Avatar asked Dec 21 '25 17:12

Rohit Patwa


1 Answers

You can try fillna or combine_first:

df.Credit_History = df.Credit_History.fillna(df.Loan_Status)

Or:

df.Credit_History = df.Credit_History.combine_first(df.Loan_Status)

Sample:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Credit_History':[1,2,np.nan, np.nan],
                   'Loan_Status':[4,5,6,8]})

print (df)
   Credit_History  Loan_Status
0             1.0            4
1             2.0            5
2             NaN            6
3             NaN            8

df.Credit_History = df.Credit_History.combine_first(df.Loan_Status)
print (df)
   Credit_History  Loan_Status
0             1.0            4
1             2.0            5
2             6.0            6
3             8.0            8
like image 120
jezrael Avatar answered Dec 24 '25 09:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!