title: counting unique values in column using sub-id
body: <p>I have a <code>df</code> containing sub-trajectories (segments) of users, with mode of travel indicated by <code>0,1,2...</code> which looks like this:</p>
  <pre><code>df = pd.read_csv('sample.csv')
  df
         id     lat          lon   mode
  0   5138001 41.144540   -8.562926   0
  1   5138001 41.144538   -8.562917   0
  2   5138001 41.143689   -8.563012   0
  3   5138003 43.131562   -8.601273   1
  4   5138003 43.132107   -8.598124   1
  5   5145001 37.092095   -8.205070   0
  6   5145001 37.092180   -8.204872   0
  7   5145015 39.289341   -8.023454   2
  8   5145015 39.197432   -8.532761   2
  9   5145015 39.198361   -8.375641   2
  </code></pre>
  <p>In the above sample, <code>id</code> is for the segments but a full trajectory maybe covered by different modes (i.e. contains multiple segments).
  So the first 4-digits of <code>id</code> is the unique trajectories, and the last 3-digits, unique segment with that trajectory.</p>
  <p>I know that I can count the number of unique segments in the <code>df</code>using:</p>
  <pre><code>df.groupby('id').['mode'].nunique()
  </code></pre>
  <p>How do I then count the number of unique trajectories <code>5138, 5145, ...</code>?</p>