填写pandas中另一列的缺失值

时间:2016-09-30 11:12:07

标签: python-2.7 pandas dataframe multiple-columns nan

我的pandas数据框中有两列。

我想用Credit_History列(dtype:int64)的值填充Loan_Status列(dtype:int64)的缺失值。

1 个答案:

答案 0 :(得分:4)

您可以尝试fillnacombine_first

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

或者:

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

样品:

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