5

I'm trying to change a format of pd data frame column without changing the type of data. Here is what I have: df = pd.DataFrame({'Age': [24.0, 32.0}])

I'd like to represent Age in 24 32 type or 24.00 32.00 and keep them as floats. Here is what I can do:

df['Age'].map('{:,.2f}'.format)

But this line changes the type of data to object. I was also trying to apply: `

df = df.style.format({'Age': '{:,.2f}'.format})`

but there is something wrong in it. Please help to figure out the right way.

4
  • I'm asking for both ways: 0.00 and without decimals at all, but keeping float type. Commented Jan 26, 2019 at 12:33
  • if i'm not mistaken format is a string method. try to use round method with the number of digits after the dot. i.e round(age,2) or round(age,0) Commented Jan 26, 2019 at 12:38
  • So, when you do df.Age it must be a float itself. Commented Jan 26, 2019 at 12:47
  • @LiorT, thanks, I try df.Age.round(2), but there is again something wrong I do. Commented Jan 26, 2019 at 12:50

3 Answers 3

14

Your dataFrame itself a type float.

Dataframe:

>>> df
    Age
0  24.0
1  32.0

Check DataFrame type:

>>> df.dtypes
Age    float64
dtype: object

check dtype for DataFrame column type:

>>> df.Age
0    24.0
1    32.0
Name: Age, dtype: float64

OR even check like:

>>> df['Age'].dtype.kind
'f'

The way you are using to round up double digit zeros that's correct but converting them again to float will get them remain in single zero as being float.

>>> df['Age'].map('{:,.2f}'.format)
0    24.00
1    32.00
Name: Age, dtype: object

As you are interested keeping either mimic like int values 24, 32 or 24.00 & 32.00, if you are only interested in the display of floats then you can do pd.set_option('display.float_format','{:.0f}'.format), which doesn't actually affect your data.

For Floating Format without leading zeros

>>> pd.set_option('display.float_format','{:.0f}'.format)
>>> df
   Age
0   24
1   32

>>> df.dtypes
Age    float64
dtype: object

For Floating Format

>>> pd.set_option('display.float_format','{:.2f}'.format)
>>> df
    Age
0 24.00
1 32.00
>>> df.dtypes
Age    float64
dtype: object

Alternative way

Set the display precision option:

>>> pd.set_option('precision', 0)
>>> df
   Age
0   24
1   32

>>> df.dtypes
Age    float64
dtype: object
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. So there is no way to keep these floats in .2f or .0f format as floats?
@Jerry, see the updated answer ,, hope that will help.
thank you so much for your help! exactly what I was looking for.
1

I believe using df.round is the best way:

>>> df = pd.DataFrame({'Age': [24.0, 32.0]})
>>> df2 = df.round({'Ages': 2})
>>> print(df2.dtypes)
>>> df2
    Age
0   24.00
1   32.00

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.round.html

Comments

0

If you want to apply to specific column of the dataframe

df["col_name"] = df["col_name"].apply(lambda x: format(float(x),".2f"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.