Is there a way to round a single column in pandas without affecting the rest of the dataframe?
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
df.value1.apply(np.round) gives
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
No need to use for loop. It can be directly applied to a column of a dataframe
sleepstudy['Reaction'] = sleepstudy['Reaction'].round(1)
Use the pandas.DataFrame.round() method like this:
df = df.round({'value1': 0})
Any columns not included will be left as is.
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))
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(...).
Source: Stackoverflow.com