Я предполагаю, что у вас есть некоторые значения в df1['tenure']
, которых нет в (0,80]
, может быть, нули.См. Пример ниже:
df1 = pd.DataFrame({'tenure':[-1, 0, 12, 34, 78, 80, 85]})
print (pd.cut(df1["tenure"] , bins=[0,20,60,80], labels=['low','medium','high']))
0 NaN # -1 is lower than 0 so result is null
1 NaN # it was 0 but the segment is open on the lowest bound so 0 gives null
2 low
3 medium
4 high
5 high # 80 is kept as the segment is closed on the right
6 NaN # 85 is higher than 80 so result is null
Name: tenure, dtype: category
Categories (3, object): [low < medium < high]
Теперь вы можете передать параметр include_lowest=True
в pd.cut
, чтобы сохранить левую границу в результате:
print (pd.cut(df1["tenure"] , bins=[0,20,60,80], labels=['low','medium','high'],
include_lowest=True))
0 NaN
1 low # now where the value was 0 you get low and not null
2 low
3 medium
4 high
5 high
6 NaN
Name: tenure, dtype: category
Categories (3, object): [low < medium < high]
Итак, наконец, ядумайте, что если вы напечатаете len(df1[(df1.tenure <= 0) | (df1.tenure > 80)])
, вы получите 11 с вашими данными как число null
значений в ваших df2
(здесь это 3 с моими данными)