对于pandas dataframe列,TypeError:float()参数必须是字符串或数字

时间:2017-07-31 13:41:02

标签: python-2.7 pandas numpy

这里的代码是' LoanAmount',' ApplicantIncome',' CoapplicantIncome'是类型对象:

TypeError: float() argument must be a string or a number

在将对象类型转换为float时出现以下错误:

Loan_ID Gender  Married Dependents  Education   Self_Employed   ApplicantIncome CoapplicantIncome   LoanAmount  Loan_Amount_Term    Credit_History  Property_Area   Loan_Status 
LP001002    Male    No  0         Graduate        No                5849            0                               360                        1                Urban           Y 
LP001003    Male    Yes 1         Graduate        No                4583            1508                128         360                        1                Rural           N 
LP001005    Male    Yes 0         Graduate        Yes               3000            0                   66          360                        1                Urban           Y 
LP001006    Male    Yes 0         Not Graduate    No                2583            2358                120         360                        1                Urban           Y

请帮助,因为我需要使用这些功能训练我的分类模型。这是csv文件的片段 -

date('d.M Y', strtotime($last_record['recorddate']))

1 个答案:

答案 0 :(得分:0)

在你的代码document = document.fillna(lambda x: x.median())中将返回一个函数而不是一个值,因此函数不能转换为float,它应该是一个数字字符串或一个整数。

希望以下代码有帮助

median = document['LoanAmount'].median()
document['LoanAmount'] = document['LoanAmount'].fillna(median) # Or document = document.fillna(method='ffill')
for col in ['LoanAmount', 'ApplicantIncome', 'CoapplicantIncome']:
    document[col]=document[col].astype(float)

document['LoanAmount_log'] = np.log(document['LoanAmount'])
document['TotalIncome'] = document['ApplicantIncome'] + document['CoapplicantIncome']
document['TotalIncome_log'] = np.log(document['TotalIncome'])