-1

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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
beginagain
  • 87
  • 1
  • 2
  • 6

1 Answers1

0

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...

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Thanks Paul and Andy. I am sort of getting an idea now. @Andy, what if the preceding vehicle class cannot be obtained by shift()? I know that shift() works fine here but if that's not the case, how can I create a new column of 'Preceding Vehicle Class' out of the data (depending upon Preceding Vehicle ID and Vehicle Class columns)? – beginagain Sep 11 '13 at 22:25
  • @UmairDurrani it's a little messier and will be slower: `df1 = df.set_index('Vehicle ID'); df1['Preced Veh ID'].apply(lambda x: df1.loc[x, 'Vehicle Class'] if (pd.notnull(x) and x != '0') else x)` – Andy Hayden Sep 11 '13 at 23:04