Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

python 2.7 - 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...