pandas:从DataFrame中打印所有非空行

时间:2015-03-11 09:35:22

标签: python pandas dataframe

我有这些数据:

time-stamp              ccount  A   B   C   D   E   F   G   H   I
2015-03-03T23:43:33+0000    0   0   0   0   0   0   0   0   0   0
2015-03-04T06:33:28+0000    0   0   0   0   0   0   0   0   0   0
2015-03-04T06:18:38+0000    0   0   0   0   0   0   0   0   0   0
2015-03-04T05:36:43+0000    0   0   0   1   0   0   0   0   0   0
2015-03-04T05:29:09+0000    0   0   0   1   0   0   0   0   1   0
2015-03-04T07:01:11+0000    0   0   1   0   1   0   0   0   0   0
2015-03-03T15:27:06+0000    19  0   1   0   1   0   0   0   0   0
2015-03-03T15:43:38+0000    10  0   1   0   1   1   0   0   0   0
2015-03-03T18:16:26+0000    0   0   0   1   0   0   0   0   0   0
2015-03-03T18:19:48+0000    0   0   0   0   0   0   0   0   0   0
2015-03-03T18:20:02+0000    4   0   0   0   0   1   0   0   0   0
2015-03-03T20:21:55+0000    2   0   0   0   0   0   1   0   0   0
2015-03-03T20:37:36+0000    0   0   0   0   0   0   0   0   0   0
2015-03-04T03:03:51+0000    1   0   0   0   0   0   1   0   0   0
2015-03-03T16:33:04+0000    9   0   0   0   0   0   0   0   0   0
2015-03-03T16:18:13+0000    1   0   0   0   0   0   0   0   0   0
2015-03-03T16:34:18+0000    4   0   0   0   0   0   0   0   0   0
2015-03-03T18:11:36+0000    5   0   0   0   0   0   0   0   0   0
2015-03-03T18:24:35+0000    0   0   0   0   0   0   0   0   0   0

我想在A到I列中切割至少有一个(" 1")的所有行。

对于上述数据,输出将为:

time-stamp              ccount  A   B   C   D   E   F   G   H   I
2015-03-04T05:36:43+0000    0   0   0   1   0   0   0   0   0   0
2015-03-04T05:29:09+0000    0   0   0   1   0   0   0   0   1   0
2015-03-04T07:01:11+0000    0   0   1   0   1   0   0   0   0   0
2015-03-03T15:27:06+0000    19  0   1   0   1   0   0   0   0   0
2015-03-03T15:43:38+0000    10  0   1   0   1   1   0   0   0   0
2015-03-03T18:16:26+0000    0   0   0   1   0   0   0   0   0   0
2015-03-03T18:20:02+0000    4   0   0   0   0   1   0   0   0   0
2015-03-03T20:21:55+0000    2   0   0   0   0   0   1   0   0   0
2015-03-04T03:03:51+0000    1   0   0   0   0   0   1   0   0   0

我们忽略了所有没有" 1"在A到I的任何一列中。

3 个答案:

答案 0 :(得分:4)

您可以使用any和布尔索引来仅选择至少有一个条目等于1的行:

df[(df.loc[:,['A','B','C','D','E','F','G','H','I']] == 1).any(axis=1)]

如果您有很多标签,请参阅标签栏有点单调乏味,这样您就可以使用切片来使事情变得更整洁:

df[(df.loc[:, 'A':'I'] == 1).any(axis=1)]

答案 1 :(得分:2)

a = open("a.txt",'r')

for line in a:
  new = line.split(" ")
  if "1" in new[1:]:
    print line

输出:

2015-03-04T05:36:43+0000    0   0   0   1   0   0   0   0   0   0

2015-03-04T05:29:09+0000    0   0   0   1   0   0   0   0   1   0

2015-03-04T07:01:11+0000    0   0   1   0   1   0   0   0   0   0

2015-03-03T15:27:06+0000    19  0   1   0   1   0   0   0   0   0

2015-03-03T15:43:38+0000    10  0   1   0   1   1   0   0   0   0

2015-03-03T18:16:26+0000    0   0   0   1   0   0   0   0   0   0

2015-03-03T18:20:02+0000    4   0   0   0   0   1   0   0   0   0

2015-03-03T20:21:55+0000    2   0   0   0   0   0   1   0   0   0

2015-03-04T03:03:51+0000    1   0   0   0   0   0   1   0   0   0

2015-03-03T16:18:13+0000    1   0   0   0   0   0   0   0   0   0

答案 2 :(得分:0)

另一种解决方案,假设A到I列中的所有值都是非负的 df[(df.drop(['time-stamp','ccount'], axis=1).sum(axis=1) > 0)]

当然删除部分可以与其他解决方案结合使用