如何在 pandas 数据框列中插入逗号作为千位分隔符? [英] How to insert a comma as a thousands separator in a pandas dataframe column?
本文介绍了如何在 pandas 数据框列中插入逗号作为千位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图格式化美元金额"列,以逗号分隔以方便查看,但我一直无法弄清楚.有人可以给我指路吗?
I'm trying to format the Dollar Amount column to have a comma thousands separator for easier viewing, but I haven't been able to figure it out. Can someone please show me the way?
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
Notice it will convert your float
type to 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
这篇关于如何在 pandas 数据框列中插入逗号作为千位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文