0

Suppose I have the following DataFrame:

d = {'col1': [1000, 2, 3, 2400000]}
df = pd.DataFrame(data=d) 

which results in

col1
1000
2
3
2400000

Suppose I would like to have this DataFrame formatting in thousands or millions, depending on the current value, how would I do this? Say I would like the result to be

col1
1K
2
3
2.4M

I know how to do it when I'd like the whole column to be formatting into millions or thousands, but not when it comes to separate values.

Whizkid95
  • 271
  • 4
  • 14

1 Answers1

2

Pass the function defined here in apply.

df.col1 = df.col1.apply(lambda x: human_format(x))
df.col1
0    1.00K
1    1.23K
2     2.00
3     3.00
4    2.40M

Optional - You can further remove .00 after this step.

meW
  • 3,832
  • 7
  • 27