I am trying to remove commas from number which has been entered as a string and change it’s data type to integer for analysis purposes
0 1,741,147
1 1,755
2 95,532
3 216,022
4 208,134
Above is the Pandas series(‘Active Cases’) that I am working with whose data type is Object. I want to remove the commas and change the data type to int64. I have tried doing it in the following way but it is not working:
df['Active Cases'].replace(',','')
df['Active Cases'].astype(np.int64)
>Solution :
The operation is not inplace, you need assign the result back
df['Active Cases'] = df['Active Cases'].replace(',','').astype(np.int64)
If you want to make the replace
more robust, like also replace possible spaces after comma, you can do Series.str.replace
df['Active Cases'] = df['Active Cases'].str.replace(', *', '', regex=True).astype(np.int64)
I would recommend do pd.to_numeric
instead to avoid possible error
df['Active Cases'] = pd.to_numeric(df['Active Cases'].str.replace(', *', '', regex=True), errors='coerce')