excel - 如何在 Pandas 数据框列中插入逗号作为千位分隔符?

标签 excel pandas dataframe python-3.6

我正在尝试格式化美元金额列以使用逗号千位分隔符以便于查看,但我无法弄清楚。有人可以告诉我路吗?

import pandas as pd
df = pd.read_excel('filename.xlsx') 
df['Dollar Amount'].head()

Index  Dollar Amount
0      5721.48
1      4000.00
2      4769.00
3       824.07
4       643.60
5       620.00

Name: Dollar Amount, dtype: float64

最佳答案

请注意,它将转换您的 float输入 object

df.DollarAmount.apply(lambda x : "{:,}".format(x))
Out[509]: 
0    5,721.48
1     4,000.0
2     4,769.0
3      824.07
4       643.6
5       620.0
Name: DollarAmount, dtype: object

关于excel - 如何在 Pandas 数据框列中插入逗号作为千位分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47404472/

相关文章:

python - 在Python中分析列的高度差并选择最大差值

python - Colab Python : Downloading/Uploading google files without mounting

python - 按时间间隔分组和计数 - Python

python - 过滤包含非 ascii 值的 Pandas 数据框行

vba - Excel VBA : Optimal use of DateValue in VBA (or other method to compare dates)

vba - 使用 VBA 在数据透视表中隐藏列标签 "Values"

html - 我该如何提取这段文字

vba - Excel VBA For 循环不起作用,不断出现错误消息

python - 10,000 到 100,000 到 150,000,000 及以上的正则表达式

python - 如何根据多个条件用字符串拆分 Pandas 数据框列