Возможно, у вас есть значение, равное 50, 60 или 70 и т. Д. c. Вы можете использовать <=
вместо <
или cut
из pandas,
import numpy as np
import pandas as pd
onefile1['quiz1'] = (onefile1['quiz1']
.astype(str).str.replace('-', '0')
.astype(float))
labels = [
0, 'lessthen50', 'between50to60',
'between60to70', 'between70to80', 'morethen80'
]
bins = [-1, 0, 50, 60, 70, 80, np.inf]
onefile1['grade'] = pd.cut(
onefile1.quiz1, bins=bins,
labels=labels, include_lowest=True)
Вот пример,
>>> import numpy as np
>>> import pandas as pd
>>> onefile1 = pd.DataFrame({'quiz1': [0, 40, 30, 60, 80, 100, '-']})
>>> onefile1['quiz1'] = (onefile1['quiz1']
.astype(str).str.replace('-', '0')
.astype(float))
>>> labels = [
0, 'lessthen50', 'between50to60',
'between60to70', 'between70to80', 'morethen80'
]
>>> bins = [-1, 0, 50, 60, 70, 80, np.inf]
>>> onefile1['grade'] = pd.cut(
onefile1.quiz1, bins=bins,
labels=labels, include_lowest=True)
>>> onefile1
quiz1 grade
0 0.0 0
1 40.0 lessthen50
2 30.0 lessthen50
3 60.0 between50to60
4 80.0 between70to80
5 100.0 morethen80
6 0.0 0
PS : Рекомендуется проверить параметры include_lowest
и right
перед использованием.