Reputation: 3583
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!
Upvotes: 1
Views: 174
Reputation: 1441
You can try this approach.
import pandas as pd
dat = pd.DataFrame({'lon': [1.1, 2.2, 3.3], 'lat': [5.5, 6.6, 7.7]})
#determine the columns and the corresponding round scale by a dictionary
dat = dat.round({'lon': 0, 'lat': 0})
The result will be:
dat=
lat lon
0 6.0 1.0
1 7.0 2.0
2 8.0 3.0
Upvotes: 0
Reputation: 394469
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
Upvotes: 2