问题描述
我有一个数据框,其中有一个名为Lead Rev的列。此列是一个数字字段,如(100000或5000等...)。我想知道如何格式化这些数字以将逗号显示为千位分隔符。
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.
是否类似于:'{:,}'。format('Lead Rev')
Is it something like: '{:,}'.format('Lead Rev')
'Lead Rev'是我的专栏名称。我的数据集中有超过200,000行。
'Lead Rev' is my column name. I have over 200,000 rows in my dataset.
ValueError Traceback(最近一次调用)最后一个)
in()
----> 1'{:,}'。format('Lead Rev')
ValueError Traceback (most recent call last) in ()----> 1 '{:,}'.format('Lead Rev')
ValueError:不能用's'指定','或'_'。
ValueError: Cannot specify ',' or '_' with 's'.
推荐答案
您可以使用apply()来获得所需的结果。这也适用于浮动
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
这篇关于将数字格式化为逗号以在Python中分隔数千个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!