Sampling in pandas
<p>If I want to randomly sample a pandas dataframe I can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sample.html" rel="nofollow">pandas.DataFrame.sample</a>.</p>

<p>Suppose I randomly sample 80% of the rows. How do I automatically get the other 20% of the rows that were not picked?</p>

<pre><code>&gt;&gt;&gt; import pandas as pd, numpy as np
&gt;&gt;&gt; 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]})
&gt;&gt;&gt; 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
&gt;&gt;&gt; sample = df.sample(5)
&gt;&gt;&gt; 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
&gt;&gt;&gt; idxs_not_in_sample = [idx for idx in df.index if idx not in sample.index]
&gt;&gt;&gt; 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
&gt;&gt;&gt; not_sample = df.loc[idxs_not_in_sample]
&gt;&gt;&gt; not_sample
    a   b
1   2  12
5   6  16
6   7  17
8   9  19
9  10  20
</code></pre>

python pandas