Round float columns in pandas dataframe

Question:

I have got the following pandas data frame

          Y         X id WP_NER
0 35.973496 -2.734554  1  WP_01 
1 35.592138 -2.903913  2  WP_02 
2 35.329853 -3.391070  3  WP_03 
3 35.392608 -3.928513  4  WP_04 
4 35.579265 -3.942995  5  WP_05 
5 35.519728 -3.408771  6  WP_06 
6 35.759485 -3.078903 7 WP_07 

I´d like to round Y and X columns using pandas.
How can I do that ?

Asked By: kamome

||

Answers:

You can apply round:

In [142]:
df[['Y','X']].apply(pd.Series.round)

Out[142]:
    Y  X
0  36 -3
1  36 -3
2  35 -3
3  35 -4
4  36 -4
5  36 -3
6  36 -3

If you want to apply to a specific number of places:

In [143]:
df[['Y','X']].apply(lambda x: pd.Series.round(x, 3))

Out[143]:
        Y      X
0  35.973 -2.735
1  35.592 -2.904
2  35.330 -3.391
3  35.393 -3.929
4  35.579 -3.943
5  35.520 -3.409
6  35.759 -3.079

EDIT
You assign the above to the columns you want to modify like the following:

In [144]:
df[['Y','X']] = df[['Y','X']].apply(lambda x: pd.Series.round(x, 3))
df

Out[144]:
        Y      X  id WP_NER
0  35.973 -2.735   1  WP_01
1  35.592 -2.904   2  WP_02
2  35.330 -3.391   3  WP_03
3  35.393 -3.929   4  WP_04
4  35.579 -3.943   5  WP_05
5  35.520 -3.409   6  WP_06
6  35.759 -3.079   7  WP_07
Answered By: EdChum

You can now, use round on dataframe

Option 1

In [661]: df.round({'Y': 2, 'X': 2})
Out[661]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07

Option 2

In [662]: cols = ['Y', 'X']

In [663]: df[cols] = df[cols].round(2)

In [664]: df
Out[664]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07
Answered By: Zero

Round is so smart that it works just on float columns, so the simplest solution is just:

df = df.round(2)
Answered By: neves

You can also – first check to see which columns are of type float – then round those columns:

for col in df.select_dtypes(include=['float']).columns:
     df[col] = df[col].apply(lambda x: x if(math.isnan(x)) else round(x,1))

This also manages potential errors if trying to round nanvalues by implementing if(math.isnan(x))

Answered By: Grant Shannon

you can do the below:

df['column_name'] = df['column_name'].apply(lambda x: round(x,2) if isinstance(x, float) else x)

that check as well if the value of the cell is a float number. if is not float return the same value. that comes from the fact that a cell value can be a string or a NAN.

Answered By: savtantalos
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.