带有千空间分离器的格式大熊猫柱

发布于 2025-02-05 18:16:56 字数 305 浏览 2 评论 0原文

我试图在任何地方看,但是我想要的东西没有确切的答案。

我知道一千的标准格式是一个逗号,但我希望有一个千分离器的空间。 我尝试了这个解决方案,但是仍然有一个逗号作为分离器:

d = {'col1': [1000, 20000], 'col2': [300000, 400000]}
df=pd.DataFrame(d)
df['col1']=df['col1'].map('{:,.0f}'.format)
df['col1'].astype(str)
df['col1'].replace(',',' ',inplace=True)

I tried to look everywhere, but there is no exact answer to what I'm looking for.

I know the standard formatting for a thousand is with a comma, but I want a space as a thousand separator.
I tried this solution, but there is still the comma as a separator:

d = {'col1': [1000, 20000], 'col2': [300000, 400000]}
df=pd.DataFrame(d)
df['col1']=df['col1'].map('{:,.0f}'.format)
df['col1'].astype(str)
df['col1'].replace(',',' ',inplace=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-02-12 18:16:56

您需要将完成的工作分配给变量,例如df ['col1']。astype('str')需要分配给df ['col1']

尝试此

d = {'col1': [1000, 20000], 'col2': [300000, 400000]}
df = pd.DataFrame(d)
for col in df.columns:
    df[col] = df[col].map('{:,.0f}'.format)
    df[col] = df[col].astype('str')
    df[col] = df[col].str.replace(',',' ')
print(df)

输出

     col1     col2
0   1 000  300 000
1  20 000  400 000

You need to assign the work done to the variable, for example df['col1'].astype('str') needs to be assigned to df['col1']

Try this

d = {'col1': [1000, 20000], 'col2': [300000, 400000]}
df = pd.DataFrame(d)
for col in df.columns:
    df[col] = df[col].map('{:,.0f}'.format)
    df[col] = df[col].astype('str')
    df[col] = df[col].str.replace(',',' ')
print(df)

output

     col1     col2
0   1 000  300 000
1  20 000  400 000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文