Python 如何从文本文件中打印选择性数据?

Python 如何从文本文件中打印选择性数据?,python,python-2.7,pandas,enthought,Python,Python 2.7,Pandas,Enthought,我只想绘制那些车辆(车辆ID)的车辆加速度和速度(简单的线形图),这些车辆是跟在卡车(车辆类别=3)后面的车辆(车辆类别=2)。 我有一个数据框,如下所示: Vehicle ID Vehicle Class Lane ID Preced Veh ID Local Y Vehicle V Vehicle A 0 j 2 2 0 6.0 45.0 0.50 1

我只想绘制那些车辆(车辆ID)的车辆加速度和速度(简单的线形图),这些车辆是跟在卡车(车辆类别=3)后面的车辆(车辆类别=2)。
我有一个数据框,如下所示:

  Vehicle ID  Vehicle Class  Lane ID Preced Veh ID  Local Y  Vehicle V  Vehicle A
0          j              2        2             0      6.0       45.0       0.50
1          i              3        2             j      5.5       37.5      -1.60
2          h              2        2             i      4.0       39.0       1.00
3          g              2        2             h      2.8       40.0       0.67
4          f              2        2             g      1.4       42.0       0.70
5          e              1        2             f      0.4       41.0       0.50
6          d              2        1             e      5.5       43.0       0.80
7          c              3        1             d      4.0       36.0      -1.30
8          b              3        1             c      2.8       34.5      -1.00
9          a              2        1             b      1.4       40.0       0.91
我无法找出只选择跟在卡车后面的汽车(2跟3)然后绘制它们的代码


注意:我创建此文件是为了便于编写代码,但实际上,文本文件有18列,超过一百万行。

您可以使用shift查看以前的车辆类别:

In [11]: df['Vehicle Class'].shift()
Out[11]: 
0   NaN
1     2
2     3
3     2
4     2
5     2
6     1
7     2
8     3
9     3
Name: Vehicle Class, dtype: float64
并选择具有2级但先前车辆为3级的车辆:

In [12]: df[(df['Vehicle Class'] == 2) & (df['Vehicle Class'].shift() == 3)]
Out[12]: 
  Vehicle ID  Vehicle Class  Lane ID Preced Veh ID  Local Y  Vehicle V  Vehicle A
2          h              2        2             i      4.0         39       1.00
9          a              2        1             b      1.4         40       0.91

我不清楚如何绘制此结果…

这是一个文本文件。我只使用excel创建图像到目前为止您尝试了什么,您发现熊猫文档的哪些元素不清楚?实际上,我不清楚我应该为选择部分做什么。我尝试了排序功能,但它只按车辆类型对数据进行排序。请阅读:谢谢Paul和Andy。我现在有点明白了@Andy,如果通过shift()无法获得前面的车辆等级,该怎么办?我知道shift()在这里可以正常工作,但如果不是这样,我如何从数据中创建一个新的“Previous Vehicle Class”列(取决于Previous Vehicle ID和Vehicle Class列)?@UmairDurrani有点混乱,速度会慢一些:
df1=df.set_index(“Vehicle ID”);df1['Preced Veh ID'].如果(pd.notnull(x)和x!=“0”)其他x)