2017-07-31 69 views
0

這裏是代碼,其中「LoanAmount」,「ApplicantIncome」,「CoapplicantIncome」是類型對象:對於大熊貓數據幀列中,類型錯誤:浮子()參數必須是字符串或數字

document=pandas.read_csv("C:/Users/User/Documents/train_u6lujuX_CVtuZ9i.csv") 


document.isnull().any() 
document = document.fillna(lambda x: x.median()) 

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']) 

我得到以下錯誤在轉換對象類型爲float:

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

請幫助,因爲我需要通過這些功能來訓練我的分類模型。這裏的CSV文件的一個片段 -

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 
+0

可以添加CSV文件的片段?並添加了錯誤 – Dark

+0

的行號! @Bharathshetty –

+0

@Bharathshetty錯誤是在訓練數據在分類器 –

回答

0

在你的代碼document = document.fillna(lambda x: x.median())將返回一個功能不那麼函數無法轉換爲浮動它應該是一個數字的字符串或整數值。

希望下面的代碼可以幫助

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']) 
+0

現在出現以下錯誤 - ValueError:輸入包含NaN,無窮大或對於dtype('float32' )。 –

+0

我提供的代碼與我的數據一起工作。 – Dark

+0

既然你有很多的列,最好使用'ffill',然後適合數據。 – Dark

相關問題