Search code examples
pythonpython-2.7pandasenthought

How to plot selective data from a text file?


I want to plot the vehicle acceleration and velocity (simple line graphs) of only those vehicles (vehicle IDs) which are cars (vehicle class = 2) following trucks (vehicle class = 3).
I have a DataFrame as follows:

  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

I can't figure out the code to SELECT only the cars following trucks (2 following 3) and then plot them.

Note: I have created this file for making it easy for me to write code, in reality, however, the text file has 18 columns and more than a million rows.


Solution

  • You can see the previous vehicle class using 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
    

    And select those which have class 2 but the previous vehicle was class 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
    

    It's not clear to me how you want to plot this result...