从 pandas 数据帧中提取子集以确保不重叠?
Extract subset from pandas dataframes ensuring no overlap?
假设我有 2 个 Pandas 数据帧 df
具有 297232 x 122
尺寸和 df_raw
具有 840380x122
尺寸。 df
已经是 df_raw
的子集。两个数据帧的索引都是 DateTime
。我想从 df
中采样 70%
个值,从 df_raw
中采样 30%
个值(如果需要可以随机采样),同时确保采样的数据帧子集做在索引方面没有重叠。
更准确地说,df_subset
将从 df
中随机选择 70%
个值,而 df_raw_subset
从 [=] 中随机选择 30%
个值13=],但是 df_subset
和 df_raw_subset
不应包含采样行方面的重叠,即它们应该具有唯一的 DateTime
索引。
所以首先我们从 df sample
,因为尺寸很小,当我们将来从另一个更大的 df 中删除它时,我们不会有问题:没有足够的数据指向 sample
df_sub=df.sample(frac=0.7, replace=False)
然后我们将 df_raw
中的索引删除 df_sub
n=int(len(df_raw)*0.3)
df_raw_sub=df_raw.drop(df_sub.index).sample(n,replace=False)
假设我有 2 个 Pandas 数据帧 df
具有 297232 x 122
尺寸和 df_raw
具有 840380x122
尺寸。 df
已经是 df_raw
的子集。两个数据帧的索引都是 DateTime
。我想从 df
中采样 70%
个值,从 df_raw
中采样 30%
个值(如果需要可以随机采样),同时确保采样的数据帧子集做在索引方面没有重叠。
更准确地说,df_subset
将从 df
中随机选择 70%
个值,而 df_raw_subset
从 [=] 中随机选择 30%
个值13=],但是 df_subset
和 df_raw_subset
不应包含采样行方面的重叠,即它们应该具有唯一的 DateTime
索引。
所以首先我们从 df sample
,因为尺寸很小,当我们将来从另一个更大的 df 中删除它时,我们不会有问题:没有足够的数据指向 sample
df_sub=df.sample(frac=0.7, replace=False)
然后我们将 df_raw
中的索引删除 df_sub
n=int(len(df_raw)*0.3)
df_raw_sub=df_raw.drop(df_sub.index).sample(n,replace=False)