2

i have a Pandas DataFrame

   Symbol  Name  ...  % Change      Volume
9     XXX  YYY   ...     -3.62  58792000.0
11    XXX  YYY   ...      0.18  58587000.0

I would like to change the values in 'Volume' to have this result

   Symbol  Name  ...  % Change      Volume
9     XXX  YYY   ...     -3.62  58.792M
11    XXX  YYY   ...      0.18  58.587M
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
betanoox
  • 81
  • 6
  • You may want to check out this answer: https://stackoverflow.com/questions/1094841/get-human-readable-version-of-file-size – Sid Kwakkel Feb 13 '21 at 03:42

2 Answers2

1

We can get to your desired result literally step by step, by adding a '$' sign, dividing the number by 1000000, and adding the MM suffix:

df['Volume'] = '$' + (df['Volume'].astype(float)/1000000).astype(str) + 'MM'

print(df)

  Symbol Name  % Change     Volume
0    XXX  YYY    -3.620  $58.792MM
1    XXX  YYY     0.180  $58.587MM

Note that the resulting 'Volume' dtype will be object:

df.dtypes

Symbol       object
Name         object
% Change    float64
Volume       object
dtype: object

Happy to see if there's a more pythonic way by others.

sophocles
  • 13,593
  • 3
  • 14
  • 33
  • I got this error `:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy` – betanoox Feb 12 '21 at 23:12
  • 1
    The code works, you just received a warning. Check out this https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas for further information. – sophocles Feb 12 '21 at 23:15
1

How about something simple like this -

(df.Volume/1000000).round(3).astype(str)+'M'

Just divide the column by a million, round it to 3 significant digits, convert to str, and then add an 'M' at the end.

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51