如何从数据集中进行采样,并在初始数据集中获取样本的索引

2024-04-26 01:04:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个形状为(1000,10)的数据集a。 我希望进行采样,以便:

B = pd.DataFrame(A).sample(frac = 0.2)

如何得到包含B的A的索引?或者我如何根据B对A进行排序,使A开头有200行B

我已经尝试过这个代码,但我不明白为什么它会给我一个错误

I = np.argwhere((A == B[:, None]).all(axis=2))[:, 1]

还是这个

np.arange(A.shape[0])[np.isin(A,B).all(axis=1)]

谢谢


Tags: 数据sample代码nonedataframe排序错误np
1条回答
网友
1楼 · 发布于 2024-04-26 01:04:34
  1. A中创建一个布尔列,告知行是否在B
  2. 我们可以通过A.index.isin(B.index)索引在B中获得行
  3. 使用新列排序并删除该列
# after defining A and B
# step 1, 2
A["isinB"] = A.index.isin(B.index)
# step 3 Trues go to front, Falses go to end
A.sort_values("isinB").drop("isinB", 1)

相关问题 更多 >