根据条件pandas python随机选择行

时间:2016-06-02 13:53:55

标签: python pandas random

我有一个小的测试数据样本:

import pandas as pd

df = {'ID': ['H900','H901','H902','','M1435','M149','M157','','M699','M920','','M789','M617','M991','H903','M730','M191'],
  'Clone': [0,1,2,2,2,2,2,2,3,3,3,4,4,4,5,5,6],
  'Length': [48,42  ,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48]}

df = pd.DataFrame(df)

看起来像:

df
Out[4]: 
      Clone   ID  Length
0       0   H900      48
1       1   H901      42
2       2   H902      48
3       2             48
4       2  M1435      48
5       2   M149      48
6       2   M157      48
7       2             48
8       3   M699      48
9       3   M920      48
10      3             48
11      4   M789      48
12      4   M617      48
13      4   M991      48
14      5   H903      48
15      5   M730      48
16      6   M191      48

我想要一个简单的脚本来选择,例如,5行,随机输出,但只包含包含ID的行,它不应该包含任何不包含ID的行。

我的剧本:

import pandas as pd
import numpy as np

df = {'ID': ['H900','H901','H902','','M1435','M149','M157','','M699','M920','','M789','M617','M991','H903','M730','M191'],
  'Clone': [0,1,2,2,2,2,2,2,3,3,3,4,4,4,5,5,6],
  'Length': [48,42  ,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48]}

df = pd.DataFrame(df)

rows = np.random.choice(df.index.values, 5)
sampled_df = df.ix[rows]

sampled_df.to_csv('sampled_df.txt', sep = '\t', index=False)

但此脚本有时会挑选出不包含ID的行

2 个答案:

答案 0 :(得分:6)

我认为您需要使用boolean indexing过滤空ID

import pandas as pd
import numpy as np

df = {'ID': ['H900','H901','H902','','M1435','M149','M157','','M699','M920','','M789','M617','M991','H903','M730','M191'],
  'Clone': [0,1,2,2,2,2,2,2,3,3,3,4,4,4,5,5,6],
  'Length': [48,42  ,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48]}

df = pd.DataFrame(df)
print (df)
df = df[df.ID != '']

rows = np.random.choice(df.index.values, 5)
sampled_df = df.ix[rows]
print (sampled_df)

答案 1 :(得分:0)

在这种情况下也可以使用查询然后采样。像这样:

df = df.query('(ID != "")').sample(n=5)