默认情况下,python使用Banker的舍入(四舍五入到最接近的偶数),即(10.5变为10,11.5变为12)。我希望我的一半总是向右舍入。 (10.5变成11,11.5变成12,12.5变成13,当然10.2变成10,10.7变成11)
import pandas as pd
import numpy as np
data = [['tom', 10.5], ['nick', 11.5], ['jack', 10.2], ['juli', 10.7]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
#df = df.round({'Age': 0})
df["Age"] = df["Age"].apply(np.round)
列年龄的期望值是:11,12,10,11
答案 0 :(得分:0)
您可以尝试一下,
>>> import pandas as pd
>>> data = [['tom', 10.5], ['nick', 11.5], ['jack', 10.2], ['juli', 10.7]]
>>> df = pd.DataFrame(data, columns = ['Name', 'Age'])
>>> def round_school(x):
i, f = divmod(x, 1)
return int(i + ((f >= 0.5) if (x > 0) else (f > 0.5)))
>>> df['Age'] = df['Age'].apply(round_school)
>>> df
Name Age
0 tom 11
1 nick 12
2 jack 10
3 juli 11