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

后端 未结 5 406
感动是毒
感动是毒 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条回答
  •  孤街浪徒
    2021-01-14 09:12

    If you want "." as thousand separator and "," as decimal separator this will works:

    Data = pd.read_Excel(path)

    Data[my_numbers] = Data[my_numbers].map('{:,.2f}'.format).str.replace(",", "~").str.replace(".", ",").str.replace("~", ".")

    If you want three decimals instead of two you change "2f" --> "3f"

    Data[my_numbers] = Data[my_numbers].map('{:,.3f}'.format).str.replace(",", "~").str.replace(".", ",").str.replace("~", ".")

提交回复
热议问题