I have a column called amount with holds values that look like this: $3,092.44 when I do dataframe.dtypes()
it returns this column as an object how can i convert this column to type int?
You can use Series.replace
or Series.str.replace
with Series.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'].astype(int)
print (dataframe)
amount
0 309244
1 309244
in regex \D
means not digit... so we can use pd.Series.str.replace
dataframe.amount.replace('\D', '', regex=True).astype(int)
0 309244
1 309244
Name: amount, dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With