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
Answer from Zero on Stack OverflowYou 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
Round is so smart that it works just on float columns, so the simplest solution is just:
df = df.round(2)
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
.round() is a pandas method and it applies to the output format rather than the underlying value. If you have floats with many decimal places you can instead apply the Python function round() to the column.
decimals = 2
df['column'] = df['column'].apply(lambda x: round(x, decimals))
You can customize for each column using a dict like
In [658]: df1.round({'a': 2, 'c': 2})
Out[658]:
a b c
0 0.11 0.3333 0.56
1 0.22 0.4444 0.67
Or, You could do
In [649]: cols = ['a', 'c']
In [650]: df1[cols] = df1[cols].round(2)
In [651]: df1
Out[651]:
a b c
0 0.11 0.3333 0.56
1 0.22 0.4444 0.67
@Zero answers works well, but to avoid receiving the warning SettingWithCopyWarning, one could also rather do
In [649]: cols = ['a', 'c']
In [650]: df1.loc[:, cols] = df1[cols].round(2)
In [651]: df1
Out[651]:
a b c
0 0.11 0.3333 0.56
1 0.22 0.4444 0.67