2017-06-10 55 views

回答

3

在正則表達式\D意味着沒有數字......因此我們可以使用pd.Series.str.replace

dataframe.amount.replace('\D', '', regex=True).astype(int) 

0 309244 
1 309244 
Name: amount, dtype: int64 
+0

感謝您對這個答案可以請你幫我最新題;我不知道該怎麼做,你是唯一一個似乎能夠幫助你的人!求求你了,謝謝你! https://stackoverflow.com/questions/44475995/remove-blacklisted-emails-pandas-type-error-returned – kwashington122

-1

你可以將它設置爲INT方式:

df['amount'] = df['amount'].astype(np.int) 

如果你想告訴Python閱讀放在第一位列如int,用途:

#assuming you're reading from a file 
pd.read_csv(file_name, dtype={'amount':np.int32}) 
0

假設你的列名是amount ,這裏是你應該做的:

dataframe['amount'] = dataframe.amount.str.replace('\$|\.|\,', '').astype(int) 
2

你可以使用Series.replaceSeries.str.replaceSeries.astype

dataframe = pd.DataFrame(data={'amount':['$3,092.44', '$3,092.44']}) 
print (dataframe) 
     amount 
0 $3,092.44 
1 $3,092.44 

dataframe['amount'] = dataframe['amount'].replace('[\$\,\.]', '', regex=True).astype(int) 

print (dataframe) 
    amount 
0 309244 
1 309244 

dataframe['amount'] = dataframe['amount'].str.replace('[\$\,\.]', '').astype(int) 

print (dataframe) 
    amount 
0 309244 
1 309244