George Liu George Liu - 23 days ago 12
Python Question

How to round dataframe columns properly?

I am trying to round two columns in a dataframe. However, the result I got is same with the original. Code below:

def lonlat_round(dataframe, decimal_places):
dataframe[['lon','lat']] = dataframe[['lon','lat']].apply(pd.to_numeric)
dataframe['lon'] = dataframe['lon'].round(decimal_places)
dataframe['lat'] = dataframe['lat'].round(decimal_places)
return dataframe

dat = pd.DataFrame({'lon': [1.1, 2.2, 3.3], 'lat': [5.5, 6.6, 7.7]})
dat_new = lonlat_round(dataframe=dat, decimal_places=0)
dat_new == dat


And it's showing all True...why is this? How to change the values instead of the appearance? Thanks!

Answer

You took a reference in your function so when you made the call to round you also modified the passed in df:

In [7]:    
dat

Out[7]:
   lat  lon
0    6    1
1    7    2
2    8    3

In [10]:    
dat_new

Out[10]:
   lat  lon
0    6    1
1    7    2
2    8    3

If you took a copy() of the passed in df then you will see that now the comparison shows False for all values:

In [11]:    
def lonlat_round(df, decimal_places):
    dataframe = df.copy()
    dataframe[['lon','lat']] = dataframe[['lon','lat']].apply(pd.to_numeric)
    dataframe['lon'] = dataframe['lon'].round(decimal_places)
    dataframe['lat'] = dataframe['lat'].round(decimal_places)
    return dataframe
dat = pd.DataFrame({'lon': [1.1, 2.2, 3.3], 'lat': [5.5, 6.6, 7.7]})
dat_new = lonlat_round(df=dat, decimal_places=0)
dat_new == dat

Out[11]:
     lat    lon
0  False  False
1  False  False
2  False  False