Easy way to add thousand separator to numbers in Python pandas DataFrame

后端 未结 5 414
感动是毒
感动是毒 2021-01-14 08:36

Assuming that I have a pandas dataframe and I want to add thousand separators to all the numbers (integer and float), what is an easy and quick way to do it?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 08:59

    Use Series.map or Series.apply with this solutions:

    df['col'] = df['col'].map('{:,}'.format)
    df['col'] = df['col'].map(lambda x: f'{x:,}')
    

    df['col'] = df['col'].apply('{:,}'.format)
    df['col'] = df['col'].apply(lambda x: f'{x:,}')
    

提交回复
热议问题