I have a dataframe with a column of numbers that look like this:
data = [291.79, 499.31, 810.93, 1164.25]
df = pd.DataFrame(data, columns=['Onset'])
Is there an elegant way to round down the odd numbers to the nearest even number? The even numbers should be rounded to the nearest even integer. This should be the output:
data = [290, 498, 810, 1164]
df = pd.DataFrame(data, columns=['Onset'])
Thank you!
I'd use modulo operator:
df['Onset_new'] = (i:=df['Onset'].astype(int)) - i % 2
print(df)
Prints:
Onset Onset_new
0 291.79 290
1 499.31 498
2 810.93 810
3 1164.25 1164
df.apply(lambda x:np.floor(x)-np.floor(x)%2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With