【发布时间】:2021-10-19 23:03:56
【问题描述】:
我正在尝试制作基于特征向量将 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.ndarray
特征向量和问题向量之间的np.Dot应该和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
如果我能找到一种方法让它给我组中的行,这似乎应该可行。
【问题讨论】:
-
欢迎堆栈溢出!请查看How to make good pandas examples 和edit 您的问题,以包含您的输入和预期输出的示例,以便我们更好地了解您要做什么
标签: python pandas dataframe numpy partitioning