Pandas Dataframe Numbers Comma Formatted
Peter Otten
__peter__ at web.de
Sat May 9 09:50:55 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.
>>> import pandas as pd
>>> df = pd.DataFrame([[12345., 67890.], [54321, "N/A"]], columns=["X","Y"])
>>> df
X Y
0 12345 67890
1 54321 N/A
[2 rows x 2 columns]
Reading the docstring for you:
>>> help(df)
|
| to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
header=True, index=True, na_rep='NaN', formatters=None, float_format=None,
sparsify=None, nanRep=None, index_names=True, justify=None,
force_unicode=None, line_width=None, max_rows=None, max_cols=None,
show_dimensions=False)
| Render a DataFrame to a console-friendly tabular output.
|
| Parameters
| ----------
| frame : DataFrame
| object to render
...
| float_format : one-parameter function, optional
| formatter function to apply to columns' elements if they are
floats
| default None
So:
>>> print(df.to_string(float_format="{:,.0f}".format))
X Y
0 12,345 67,890
1 54,321 N/A
More information about the Python-list
mailing list