Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python数据帧中仅选择某个标签后的3行?_Python_Pandas - Fatal编程技术网

如何在python数据帧中仅选择某个标签后的3行?

如何在python数据帧中仅选择某个标签后的3行?,python,pandas,Python,Pandas,我有这样的数据: Month Time Label Apr 19:10:11 Query Apr 19:10:11 Not Command Apr 19:10:12 Not Command Apr 19:10:12 Sending Apr 19:10:13 Not Command Apr 19:10:13 Not Command Apr 19:10:14

我有这样的数据:

Month   Time          Label
Apr     19:10:11      Query
Apr     19:10:11      Not Command
Apr     19:10:12      Not Command
Apr     19:10:12      Sending
Apr     19:10:13      Not Command
Apr     19:10:13      Not Command
Apr     19:10:14      Not Command
Apr     19:10:14      Not Command
Apr     19:10:14      Answer
Apr     19:10:14      Not Command
Apr     19:10:15      Not Command
Apr     19:10:15      Not Command
我想将表放入每个标签中,这些标签不等于
not Command
,比如
Query
Sending
Answer
。我想选择标签后始终有3行。这就是我想要的结果

Month   Time          Label
Apr     19:10:11      Query
Apr     19:10:11      Not Command
Apr     19:10:12      Not Command
Apr     19:10:12      Sending
Apr     19:10:13      Not Command
Apr     19:10:13      Not Command
Apr     19:10:14      Answer
Apr     19:10:14      Not Command
Apr     19:10:15      Not Command

可能吗?我有一个想法,使新的标签,迭代数字时,遇到标签,但我不知道如何。所以也许有更好的方法来解决这个问题。感谢您

如果您认为选择
3
或更少的行创建掩码依据,并使用以下内容进行筛选:

如果需要,请选择3行(如果存在3行或更多行),否则不要选择添加新掩码,并按位过滤
-
&

print (df)
   Month      Time        Label
0    Apr  19:10:11        Query
1    Apr  19:10:11  Not Command <- only 2 rows - omited
2    Apr  19:10:12      Sending
3    Apr  19:10:13  Not Command
4    Apr  19:10:13  Not Command
5    Apr  19:10:14  Not Command
6    Apr  19:10:14  Not Command
7    Apr  19:10:14       Answer
8    Apr  19:10:14  Not Command
9    Apr  19:10:15  Not Command
10   Apr  19:10:15  Not Command

s = df['Label'].ne('Not Command').cumsum()

df = df[s.map(s.value_counts()).ge(3) & s.groupby(s).cumcount().lt(3)]
print (df)
  Month      Time        Label
2   Apr  19:10:12      Sending
3   Apr  19:10:13  Not Command
4   Apr  19:10:13  Not Command
7   Apr  19:10:14       Answer
8   Apr  19:10:14  Not Command
9   Apr  19:10:15  Not Command
打印(df)
月份时间标签
4月0日19:10:11查询

4月1日19:10:11不是命令是的,有可能。但让我们看看你到目前为止都做了些什么。
print (df)
   Month      Time        Label
0    Apr  19:10:11        Query
1    Apr  19:10:11  Not Command <- only 2 rows - omited
2    Apr  19:10:12      Sending
3    Apr  19:10:13  Not Command
4    Apr  19:10:13  Not Command
5    Apr  19:10:14  Not Command
6    Apr  19:10:14  Not Command
7    Apr  19:10:14       Answer
8    Apr  19:10:14  Not Command
9    Apr  19:10:15  Not Command
10   Apr  19:10:15  Not Command

s = df['Label'].ne('Not Command').cumsum()

df = df[s.map(s.value_counts()).ge(3) & s.groupby(s).cumcount().lt(3)]
print (df)
  Month      Time        Label
2   Apr  19:10:12      Sending
3   Apr  19:10:13  Not Command
4   Apr  19:10:13  Not Command
7   Apr  19:10:14       Answer
8   Apr  19:10:14  Not Command
9   Apr  19:10:15  Not Command