Python Dataframe dynamically

Asked

Viewed 51 times

1

I have data with the following columns: "Date","Codproduct","Sale","Quantity" I need to create several data frames in python, filtering product and selecting the other columns and always saved with the product name, for example: dates_123, dates_324. I need something like this, wearing pandas:

dados_123 = dados.loc[dados.Codproduto==123, ['Data','Venda','Quantidade']]

The problem is that I have 50 distinct Codproduto and I needed to do this dynamically to filter each Codproduto and save now with the suffix of Codproduto.

Any idea how to do that?

  • Bruno, good evening! Is there a specific purpose to create several variables? tell your final goal, maybe there is a more interesting option. Hug!

  • Good morning! I want to make a demand projection model using Prophet, but to use it, it must contain only two variables (ds and y), date and sale, respectively. I would like to make predictions for all products, but in the prophet I did not find a function that let me set that should make one model per product, so I thought separating in several df would be better.

1 answer

1

I believe you can create a 'temporary' data frame, make the predictions and save in an xlsx for example:

for cod in dados['Codproduto'].unique():
    df_temp = dados[dados['Codproduto'].isin([cod])][['Data','Quantidade']]
    df_temp.columns = ['ds','y']
    m = Prophet(daily_seasonality=True)
    m.fit(df_temp)
    future = m.make_future_dataframe(periods=10)
    forecast = m.predict(future)
    forecast.to_excel(f'dados_{cod}.xlsx')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.