中填充另一列中某一列的缺失值

中填充另一列中某一列的缺失值

本文介绍了在 pandas 中填充另一列中某一列的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的熊猫数据框中有两列.

I have two columns in my pandas dataframe.

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

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

推荐答案

您可以尝试 fillna combine_first :

You can try fillna or combine_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

这篇关于在 pandas 中填充另一列中某一列的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:48