Python/Pandas: Converting Numbers By Comma Separated For Thousands
I have a dataframe with a column containing long numbers. I am trying to convert all the values in the numbers column to comma separated for thousands. df col_1 col_2 Rooney
Solution 1:
You can use string formatting,
df['col_2'] = pd.to_numeric(df['col_2'].fillna(0), errors='coerce')
df['col_2'] = df['col_2'].map('{:,.2f}'.format)
Do remember that the col_2 now will be string not integer.
col_1 col_2
0 Rooney 34,590,927.00
1 Ronaldo 5,467,382.00
2 John 25,647,398.00
Solution 2:
apply format function to col_2
df = df.assign(col_2=df.col_2.astype(int).apply('{:,}'.format))
col_1 col_2
0 Rooney 34,590,927
1 Ronaldo 5,467,382
2 John 25,647,398
Post a Comment for "Python/Pandas: Converting Numbers By Comma Separated For Thousands"