python: AttributeError: float object has no attribute 'between'
python: AttributeError: float object has no attribute 'between'
给定数据框的一列,将其数值分成 10 组后,我试图为每个组分配一个标签,并创建一个由这些标签组成的列表。
为此,我需要检查此列中的每个值位于哪个间隔之间,
然而,根据我得到的错误
AttributeError: float object has no attribute 'between'
没有 'between' 命令来处理这个问题。
l2=[29.69911764705882, 32.5, 32.5, 54.0, 12.0, 29.69911764705882, 24.0, 29.69911764705882, 45.0, 33.0, 20.0, 47.0, 29.0,
25.0, 23.0, 19.0, 37.0, 16.0, 24.0, 29.69911764705882, 22.0, 24.0, 19.0, 18.0, 19.0, 27.0, 9.0, 36.5, 42.0, 51.0, 22.0,
55.5, 40.5, 29.69911764705882, 51.0, 16.0, 30.0, 29.69911764705882, 29.69911764705882, 44.0, 40.0, 26.0, 17.0, 1.0, 9.0,
29.69911764705882, 45.0, 29.69911764705882, 28.0, 61.0, 4.0, 1.0, 21.0, 56.0, 18.0, 29.69911764705882, 50.0, 30.0, 36.0,
29.69911764705882, 29.69911764705882, 9.0, 1.0, 4.0, 29.69911764705882, 29.69911764705882, 45.0, 40.0, 36.0, 32.0, 19.0,
19.0, 3.0, 44.0, 58.0, 29.69911764705882, 42.0, 29.69911764705882, 24.0, 28.0, 29.69911764705882, 34.0, 45.5, 18.0, 2.0,
32.0, 26.0, 16.0, 40.0, 24.0, 35.0, 22.0, 30.0, 29.69911764705882, 31.0, 27.0, 42.0, 32.0, 30.0, 16.0, 27.0, 51.0,
29.69911764705882, 38.0, 22.0, 19.0, 20.5, 18.0, 29.69911764705882, 35.0, 29.0, 59.0, 5.0, 24.0, 29.69911764705882,
44.0, 8.0, 19.0, 33.0, 29.69911764705882, 29.69911764705882, 29.0, 22.0, 30.0, 44.0, 25.0, 24.0, 37.0, 54.0,
29.69911764705882, 29.0, 62.0, 30.0, 41.0, 29.0, 29.69911764705882, 30.0, 35.0, 50.0, 29.69911764705882, 3.0]
d = {'col1': []}
df = pd.DataFrame(data=d)
df['col1']=l2
print(df['col1'])
df['col2'] = pd.cut(df.col1,10)
print(df['col2'].value_counts())
new_list=[]
labels=['25-31','19,25','13-19','31-37','0-7','37-43','43-49','49-55','7-13','55-62']
for i in df['col1']:
for j in df['col2'].value_counts():
if i.between(j):
new_list.append(inter_list.index(j))
print(new_list)
根据pandas.cut,可以直接在函数调用中指定标签。 return 值将是一个 pandas 系列,其中包含 df.col1
中每个值的所属标签。以下代码可以为您解决问题:
labels = ['25-31', '19,25', '13-19', '31-37', '0-7',
'37-43', '43-49', '49-55', '7-13', '55-62']
df['labels'] = pd.cut(df.col1,10, labels=labels)
给定数据框的一列,将其数值分成 10 组后,我试图为每个组分配一个标签,并创建一个由这些标签组成的列表。 为此,我需要检查此列中的每个值位于哪个间隔之间, 然而,根据我得到的错误
AttributeError: float object has no attribute 'between'
没有 'between' 命令来处理这个问题。
l2=[29.69911764705882, 32.5, 32.5, 54.0, 12.0, 29.69911764705882, 24.0, 29.69911764705882, 45.0, 33.0, 20.0, 47.0, 29.0,
25.0, 23.0, 19.0, 37.0, 16.0, 24.0, 29.69911764705882, 22.0, 24.0, 19.0, 18.0, 19.0, 27.0, 9.0, 36.5, 42.0, 51.0, 22.0,
55.5, 40.5, 29.69911764705882, 51.0, 16.0, 30.0, 29.69911764705882, 29.69911764705882, 44.0, 40.0, 26.0, 17.0, 1.0, 9.0,
29.69911764705882, 45.0, 29.69911764705882, 28.0, 61.0, 4.0, 1.0, 21.0, 56.0, 18.0, 29.69911764705882, 50.0, 30.0, 36.0,
29.69911764705882, 29.69911764705882, 9.0, 1.0, 4.0, 29.69911764705882, 29.69911764705882, 45.0, 40.0, 36.0, 32.0, 19.0,
19.0, 3.0, 44.0, 58.0, 29.69911764705882, 42.0, 29.69911764705882, 24.0, 28.0, 29.69911764705882, 34.0, 45.5, 18.0, 2.0,
32.0, 26.0, 16.0, 40.0, 24.0, 35.0, 22.0, 30.0, 29.69911764705882, 31.0, 27.0, 42.0, 32.0, 30.0, 16.0, 27.0, 51.0,
29.69911764705882, 38.0, 22.0, 19.0, 20.5, 18.0, 29.69911764705882, 35.0, 29.0, 59.0, 5.0, 24.0, 29.69911764705882,
44.0, 8.0, 19.0, 33.0, 29.69911764705882, 29.69911764705882, 29.0, 22.0, 30.0, 44.0, 25.0, 24.0, 37.0, 54.0,
29.69911764705882, 29.0, 62.0, 30.0, 41.0, 29.0, 29.69911764705882, 30.0, 35.0, 50.0, 29.69911764705882, 3.0]
d = {'col1': []}
df = pd.DataFrame(data=d)
df['col1']=l2
print(df['col1'])
df['col2'] = pd.cut(df.col1,10)
print(df['col2'].value_counts())
new_list=[]
labels=['25-31','19,25','13-19','31-37','0-7','37-43','43-49','49-55','7-13','55-62']
for i in df['col1']:
for j in df['col2'].value_counts():
if i.between(j):
new_list.append(inter_list.index(j))
print(new_list)
根据pandas.cut,可以直接在函数调用中指定标签。 return 值将是一个 pandas 系列,其中包含 df.col1
中每个值的所属标签。以下代码可以为您解决问题:
labels = ['25-31', '19,25', '13-19', '31-37', '0-7',
'37-43', '43-49', '49-55', '7-13', '55-62']
df['labels'] = pd.cut(df.col1,10, labels=labels)