1

i am working with python , so in my dataframe i have a column named Company Profit and values of this column are 33536310, 842925200,.. etc.

I want to convert all values to be comma separated every three digits, like: 231,535,366 etc in whole column

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
wm 50
  • 27
  • 6
  • 1
    have you tried anything? – Zabir Al Nazi Apr 13 '20 at 14:16
  • will it work if i try like slicing last 3 digit as string and concatenate comma? – wm 50 Apr 13 '20 at 14:19
  • 1
    Provide a data sample (in text), and minimal code. – Zabir Al Nazi Apr 13 '20 at 14:20
  • Country China 1367643161.29 United States 317615384.615 Japan 127403595.973 United Kingdom 63870967.7419 Russian Federation 143500000.0 Canada 35233864.8649 Germany 80379996.9697 India 1276730769.23 France 63837349.3976 South Korea 49805429.8643 Italy 59908256.8807 Spain 46443396.2264 Iran 77075630.2521 Australia 23316017.316 Brazil 205915254.237 – wm 50 Apr 13 '20 at 14:22
  • sorry column cant fit in – wm 50 Apr 13 '20 at 14:23
  • i want to make digits comma separated – wm 50 Apr 13 '20 at 14:23
  • 1
    this is one column of data frame – wm 50 Apr 13 '20 at 14:24
  • Does this answer your question? [How to insert a comma as a thousands separator in a pandas dataframe column?](https://stackoverflow.com/questions/47404472/how-to-insert-a-comma-as-a-thousands-separator-in-a-pandas-dataframe-column) – rpanai Apr 13 '20 at 14:34

2 Answers2

0

Something like this will work:

In [601]: def thousand_separator(val): 
     ...:     return f"{val:,}"  
     ...: 

In [602]: df['Company Profit'] = df['Company Profit'].apply(thousand_separator) 
In [603]: df['Company Profit']                                                                                                                                                              
Out[602]: 
0     33,536,310
1    842,925,200
Name: Profit, dtype: object
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
0

Alternatively you can do this with f-string in newer version of Python. Use comma as a separator:

df = pd.DataFrame({'Company Profit':[33536310,842925200,231535366]})
df['Company Profit'] = df['Company Profit'].apply(lambda x: f"{x:,}")
print(df)

  Company Profit
0     33,536,310
1    842,925,200
2    231,535,366
ManojK
  • 1,570
  • 2
  • 9
  • 17