-1

enter image description herenew to Python, would appreciate any help with Pandas to manipulate output from Prophet Library ( see image ). My input Dataframe has 3 columns, Prophet only takes 2, and my output is 4 columns. Is there also a way to loop back around and run the same for Operators 5 and 6, or do I need to add them sequntially to the code ? Thanks in advance for your help. Gav

import pandas as pd
from fbprophet import Prophet
df = pd.read_csv('C:\path\myfile.csv')
df.columns = ['Operator','ds','y']
df['ds'] = pd.to_datetime(df['ds'])
dfprop=df[df['Operator']==1]
dfprop=dfprop[['ds','y']]
m = Prophet()
m.fit(dfprop)
future = m.make_future_dataframe(periods=158)
forecast = m.predict(future)
forecast
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158)
dfout = df.append(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158))

enter image description here

Gavin
  • 147
  • 2
  • 3
  • 13
  • post the code you have currently as well as a minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example – fthomson Jul 28 '21 at 17:01

1 Answers1

1

Cant test this without a reproducible dataset but something like this should do it.

import pandas as pd
from fbprophet import Prophet
df = pd.read_csv('C:\path')
df.columns = ['Operator','ds','y']
df['ds'] = pd.to_datetime(df['ds'])

def forecast_data(g):
    data = g[['ds','y']]
    m = Prophet()
    m.fit(data)
    future = m.make_future_dataframe(periods=158)
    forecast = m.predict(future)
    forecast['Operator'] = g['Operator'].iloc[0]
    forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158)
    dfout = g.append(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158))
    return dfout

df.groupby('Operator').apply(forecast_data)

apply to the group 'Operator' and create a model for each

fthomson
  • 773
  • 3
  • 9
  • Awesome thank you !, will give a try, appreciate the help. – Gavin Jul 28 '21 at 19:09
  • @Gavin let me know how it goes – fthomson Jul 28 '21 at 19:19
  • Thank you for your help, much appreciated – Gavin Jul 29 '21 at 13:21
  • @ftthomson it's nearly there, is there a way to pass the Operator number into the forecast dataframe as it goes ?, I tried adding forecast['Operator']=g – Gavin Jul 29 '21 at 14:25
  • Tried this, but it doesn't like it... forecast.insert(loc=0,column="Operator",value=g) forecast[['Operator','ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158) dfout = g.append(forecast[['Operator','ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(158)) – Gavin Jul 29 '21 at 14:56
  • Also tried this... forecast.insert(loc=0,column="Operator",value=g['Operator']) – Gavin Jul 29 '21 at 16:01
  • @Gavin see updated answer – fthomson Jul 29 '21 at 16:18
  • thank you so much for your help, it really is appreciated, it is working perfectly now :-) – Gavin Jul 29 '21 at 16:30
  • @Gavin no worries glad it helped :) if this helped feel free to accept it as the answer and upvote. Happy coding! – fthomson Jul 29 '21 at 16:32