title: Group the dataframe based on their ids and stick the values of ids to each other with the same values of the day before or the dayafter
body: <p>I have a data frame, I want to group the data based on the ids and then stick the values for each id together. However, since the number of the ids are not equal, I have to fill the missing values for those <strong>ids</strong> with <strong>same values</strong> of the day before (<strong>or the day after</strong>) of <strong>those ids</strong>. I previously ask this question and I got a response, however it is only for the day before. But, in my real data, since some times I don't have the first day, their values are Nan.</p>
  <p>I have provided an example as below, for example, for <strong>id=1, id=2 there is two dates, but for id=3</strong>, there are three days.</p>
  <pre><code>df = pd.DataFrame()
  df['id'] = [1, 1, 2,2, 3, 3, 3]
  df['date'] = ['2019-01-02', '2019-01-03', '2019-01-01','2019-01-02', '2019-01-01',      '2019-01-02','2019-01-03']
  df['val1']  = [10, 100, 20, 30, 40, 50, 60]
  df['val2']  = [30, 30, -20, -30, -40,-50, -60 ]
  df['val3']  = [50, 10, 120, 300, 140, 150, 160]
  </code></pre>
  <p>The desired output is:</p>
  <pre><code>    val0  val1   val2  val3  val4   val5   val6  val7   val8
  id                                                          
  1   10.0  30.0   50.0  10.0  30.0   50.0  100.0  30.0   10.0
  2   20.0 -20.0  120.0  30.0 -30.0  300.0   30.0 -30.0  300.0
  3   40.0 -40.0  140.0  50.0 -50.0  150.0   60.0 -60.0  160.0
  </code></pre>
  <p>Here is the response for the case where it is for the day before.</p>
  <pre><code>mi = pd.MultiIndex.from_product([df['id'].unique(), df['date'].unique()], names=['id', 'date'])
  df = df.set_index(['id', 'date']).reindex(mi).sort_index().ffill()
  df = df.groupby(level='id').apply(np.ravel).apply(pd.Series).add_prefix('val')
  </code></pre>
  <p>If you run the code, you can see that the <strong>first values for the id=1 are Nan</strong>.
  And also I don't want to sort the id.</p>
  <p>Can anybody help me with that? Thanks</p>