Home > Blockchain >  Split dataframe on multiple columns and values
Split dataframe on multiple columns and values

Time:10-30

I am trying to loop through a dataframe to create multiple dataframes (unknown number) based on multiple columns using groupby:

    df_sliced = {}
for product, value in df['Product'].unique() and df['Value'].unique():
    df_sliced[product, value] = df.groupby(['Product','Value'])

I get the error:

ValueError: too many values to unpack (expected 2)

Is there a better way of doing this?

CodePudding user response:

Is this what you are trying to accomplish?

df_sliced = {prod_val: group for prod_val, group in df.groupby(['Product','Value'])}
  • Related