大熊猫的采样

时间:2016-09-30 23:25:08

标签: python pandas

如果我想随机抽样一个pandas数据帧,我可以使用pandas.DataFrame.sample

假设我随机抽取了80%的行。如何自动获取未被挑选的其他20%的行?

2 个答案:

答案 0 :(得分:3)

正如Lagerbaer所解释的那样,可以向数据帧添加具有唯一索引的列,或者随机地随机播放整个数据帧。对于后者,

df.reindex(np.random.permutation(df.index))

的工作原理。 (np意味着numpy)

答案 1 :(得分:2)

>>> import pandas as pd, numpy as np
>>> df = pd.DataFrame({'a': [1,2,3,4,5,6,7,8,9,10], 'b': [11,12,13,14,15,16,17,18,19,20]})
>>> df
    a   b
0   1  11
1   2  12
2   3  13
3   4  14
4   5  15
5   6  16
6   7  17
7   8  18
8   9  19
9  10  20

# randomly sample 5 rows
>>> sample = df.sample(5)
>>> sample
   a   b
7  8  18
2  3  13
4  5  15
0  1  11
3  4  14

# list comprehension to get indices not in sample's indices
>>> idxs_not_in_sample = [idx for idx in df.index if idx not in sample.index]
>>> idxs_not_in_sample
[1, 5, 6, 8, 9]

# locate the rows at the indices in the original dataframe that aren't in the sample
>>> not_sample = df.loc[idxs_not_in_sample]
>>> not_sample
    a   b
1   2  12
5   6  16
6   7  17
8   9  19
9  10  20