gpt4 book ai didi

python-2.7 - 从 Pandas 的另一列中填充一列的缺失值

转载 作者:行者123 更新时间:2023-12-04 17:20:50 26 4
gpt4 key购买 nike

我的 pandas 数据框中有两列。

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

最佳答案

你可以试试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

关于python-2.7 - 从 Pandas 的另一列中填充一列的缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39790183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com