I have a dataframe which has a column called "Lead Rev" This column is a field of numbers such as (100000 or 5000 etc..). I want to know how to format these numbers to show commas as thousand separators.
Is it something like: '{:,}'.format('Lead Rev')
'Lead Rev' is my column name. I have over 200,000 rows in my dataset.
This is my error:
ValueError Traceback (most recent call last)
in ()
----> 1 '{:,}'.format('Lead Rev')
ValueError: Cannot specify ',' or '_' with 's'.
解决方案
You can use apply() to get the desired result. This works with floating too
import pandas as pd
series1 = pd.Series({'Value': 353254})
series2 = pd.Series({'Value': 54464.43})
series3 = pd.Series({'Value': 6381763761})
df = pd.DataFrame([series1, series2, series3])
print(df.head())
Value
0 3.532540e+05
1 5.446443e+04
2 6.381764e+09
df['Value'] = df.apply(lambda x: "{:,}".format(x['Value']), axis=1)
print(df.head())
Value
0 353,254.0
1 54,464.43
2 6,381,763,761.0