round a single column in pandas

PythonPandas

Python Problem Overview


Is there a way to round a single column in pandas without affecting the rest of the dataframe?

>>> print(df)
  item  value1  value2
0    a    1.12     1.3
1    a    1.50     2.5
2    a    0.10     0.0
3    b    3.30    -1.0
4    b    4.80    -1.0

I have tried the following:

>>> df.value1.apply(np.round)
0    1
1    2
2    0
3    3
4    5
5    5

What is the correct way to make data look like this:

  item  value1  value2
0    a       1     1.3
1    a       2     2.5
2    a       0     0.0
3    b       3    -1.0
4    b       5    -1.0
5    c       5     5.0

Python Solutions


Solution 1 - Python

You are very close. You applied the round to the series of values given by df.value1. The return type is thus a Series. You need to assign that series back to the dataframe (or another dataframe with the same Index).

Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).

>>> df.value1 = df.value1.round()
>>> print df
  item  value1  value2
0    a       1     1.3
1    a       2     2.5
2    a       0     0.0
3    b       3    -1.0
4    b       5    -1.0

Solution 2 - Python

For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.

decimals = 2    
df['column'] = df['column'].apply(lambda x: round(x, decimals))

Solution 3 - Python

Use the pandas.DataFrame.round() method like this:

df = df.round({'value1': 0})

Any columns not included will be left as is.

Solution 4 - Python

No need to use for loop. It can be directly applied to a column of a dataframe

sleepstudy['Reaction'] = sleepstudy['Reaction'].round(1)

Solution 5 - Python

If you are doing machine learning and use tensorflow, many float are of 'float32', not 'float64', and none of the methods mentioned in this thread likely to work. You will have to first convert to float64 first.

x.astype('float')

before round(...).

Solution 6 - Python

No need to use lambda or creating function. It is straight-forward. See example below

df['decimal_place_2'] = df['decimal_place_2'].round(2)

Solution 7 - Python

saldo_acred['cumsum_prc_saldo'].astype('float').round(2)

Solution 8 - Python

In my case, I have both string values as well as decimal values in single columns.

def round_2(x):
    try:
        return round(x,2)
    except:
        return x

df['cur_TMIN_IMD_WeekSum']=df['cur_TMIN_IMD_WeekSum'].apply(round_2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionk3itView Question on Stackoverflow
Solution 1 - PythonFrames Catherine WhiteView Answer on Stackoverflow
Solution 2 - PythonReimarView Answer on Stackoverflow
Solution 3 - PythonSiddiView Answer on Stackoverflow
Solution 4 - PythonPremalView Answer on Stackoverflow
Solution 5 - PythonkawingkelvinView Answer on Stackoverflow
Solution 6 - PythonEmmanuel OgungbemiView Answer on Stackoverflow
Solution 7 - PythonJoselin CeronView Answer on Stackoverflow
Solution 8 - PythonRamesh PonnusamyView Answer on Stackoverflow