title: Python create linear regression prediction pandas dataframe by group
body: <p>I want to create a python pandas dataframe column based on a linear regression loop</p>
  
  <p>This is the source data pandas dataframe df:</p>
  
  <pre><code>campaign    |     date     |    shown 
     A           2015-10-11       363563
     A           2015-10-12       345657
     A           2015-10-13       345346
     B           2015-10-11       23467
     B           2015-10-15       357990
     C           2015-10-11       97808
  </code></pre>
  
  <p>I want to user linear regression and for each group predict the amount shown for 2015-11-30. </p>
  
  <p>So this is the final new prediction dataframe I am looking for: </p>
  
  <pre><code> campaign |   Prediction(2015-11-30)
        A           ...
        B           ...
        C           ...
  </code></pre>
  
  <p>my code so far: </p>
  
  <pre><code>df['date_ordinal'] = df['date'].apply(lambda x: x.toordinal())
  model = LinearRegression()
  X = df[['date_ordinal']]
  y = df.shown
  model.fit(X, y)   
  
  df_results = pd.DataFrame()
  for (group, df_gp) in df.groupby('campaign'):
     df_results['campaign'] = group
     X=df_gp[['date_ordinal']]
     y=df_gp.shown
     model.fit(X,y)
     coefs = list(zip(X.columns, model.coef_))
     df_results['prediction'] = model.predict(735947)
  
  df_results
  </code></pre>
  
  <p>However, when I run this code, I only get one prediction, I don't get a dataframe with one column "group" and the predicted values next to it. </p>
  
  <p>Thank You!</p>