在 Python 如何使用 Pandas 来操纵 Prophet 的输出
In Python how to use Pandas to manipulate output from Prophet
Python 的新手,将不胜感激任何有关 Pandas 的帮助,以操纵 Prophet Library 的输出(见图)。
我的输入 Dataframe 有 3 列,Prophet 只需要 2 列,而我的输出是 4 列。
是否还有一种方法可以循环并 运行 与运算符 5 和 6 相同,或者我是否需要将它们依次添加到代码中?在此先感谢您的帮助。加夫
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))
无法在没有可重现数据集的情况下对此进行测试,但像这样的东西应该可以做到。
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)
申请小组'Operator'
并为每个人创建模型
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))
无法在没有可重现数据集的情况下对此进行测试,但像这样的东西应该可以做到。
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)
申请小组'Operator'
并为每个人创建模型