Pandas Dataframe Numbers Comma Formatted

Python python at python.invalid
Sat May 9 09:46:27 EDT 2020


Joydeep C wrote:
> On Sat, 09 May 2020 14:42:43 +0200, Python wrote:
> 
>> Joydeep wrote:
>>> I have a Pandas dataframe like below.
>>>
>>>       X        Y
>>> 0  12345    67890 1  54321    N/A 2  67890    123456
>>>
>>> I need to make these numbers comma formatted. For example, 12345 =>
>>> 12,345.
>>
>>   >>> value = 12345 f'{value:,}'  # >= 3.6
>> '12,345'
>>   >>> '{:,}'.format(value)  # >= 2.7
>> '12,345'
> 
> I need all the numbers in the whole dataframe to be formatted like that,
> not one value.

 >>> data.applymap((lambda x: f"{x:,}") )
         X         Y
0  12,345  67,890.0
1  54,321       nan
2  67,890  12,345.0
 >>> data.apply(np.vectorize((lambda x: f"{x:,}")))
         X         Y
0  12,345  67,890.0
1  54,321       nan
2  67,890  12,345.0



More information about the Python-list mailing list