如何将 Pandas 中的数字列转换为带有逗号分隔符的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40578104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:25:46 来源:igfitidea点击:
How to convert a numeric column in pandas to a string with comma separators?
提问by Want to try everything
I want to convert a column that has values like 1234567.89 to 1,234,567.89. Can someone help me with this.
我想将具有 1234567.89 之类的值的列转换为 1,234,567.89。有人可以帮我弄这个吗。
回答by Diego Mora Cespedes
You can format your column by doing this:
您可以通过执行以下操作来格式化您的列:
df['new_column_name'] = df['column_name'].map('{:,.2f}'.format)
But keep in mind that the new column will contain strings, not floats.
但请记住,新列将包含字符串,而不是浮点数。