I have a dataframe which contains a column like below,
JavaScript
x
19
19
1
total_hours
2
3
8.31,
4
5.15,
5
10.53,
6
8.69,
7
10.57,
8
10.53
9
14.25,
10
10.55,
11
0.0,
12
10.73,
13
8.54,
14
10.55,
15
0.0,
16
10.53,
17
10.52,
18
10.54
19
I have to apply condition after decimal point, Those conditions are, ( (<50)it will replace .0), and (>50) it will replace .5)). How can i do it properly??? Thanks in advance,….
Advertisement
Answer
Just implement your own rounding criteria in a function, and apply the function on required column:
JavaScript
1
6
1
def customRound(val):
2
fraction = val-int(val)
3
return int(val)+0.5 if fraction>=0.5 else int(val)
4
5
df['total_hours']=df['total_hours'].apply(customRound)
6
OUTPUT:
JavaScript
1
18
18
1
total_hours
2
0 8.0
3
1 5.0
4
2 10.5
5
3 8.5
6
4 10.5
7
5 10.5
8
6 14.0
9
7 10.5
10
8 0.0
11
9 10.5
12
10 8.5
13
11 10.5
14
12 0.0
15
13 10.5
16
14 10.5
17
15 10.5
18