将pandas数据框划分为两个子集



我正在尝试创建一个函数,该函数基于特征向量将pandas数据框划分为两个子集。

我的数据框由两列组成,其中包含一个ndarray[10000](我的特征向量)和一个整数(表示向量的标签)。

问题只是检查特征向量的索引>= 1

我已经尝试过这种方法,它是有效的,但是对于我的用例来说,它是一种缓慢的方式。

def partition( dataset, question):
true_rows, false_rows =[],[]
for row in dataset.iterrows():
if question.match(row[1][0]): 
true_rows.append(row[1])
else:
false_rows.append(row[1])
return pd.DataFrame.from_dict(true_rows), pd.DataFrame.from_dict(false_rows)

我找到了一种我认为可能有效的方法,但当我调用g.get_group()

时,我得到以下错误

TypeError: unhashable type: 'numpy. narray

np。特征向量和问题向量之间的点的作用应该与match

相同。
def partition(dataset, question):
df = dataset
# making a mask dataframe with label True or False
mask = df.apply(lambda x: np.dot(x[0], question.vector)>= 1)
df['mask'] = mask
g = df.groupby('mask')
true_rows = g.get_group(True)
false_rows = g.get_group

如果我能找到一种方法让它给我分组中的行,这似乎应该工作。

好了,我明白了。由于某些原因,当我的列有聋哑名称(数字)时,它将无法工作。

df = df.rename(columns={0:'vector', 1:'label'})

对我正在发送的数据集执行此操作,并且成功了。