Home > database >  Combine variables from a dataframe
Combine variables from a dataframe

Time:02-07

Good morning, I have the following dataframe:

dft
Country Brazil  Brazil  Brazil  Brazil  Bolivia Bolivia Bolivia Bolivia Bolivia Bolivia Bolivia Bolivia Chile   Chile   Chile   Chile   Chile   Chile   Chile   Chile
Order   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20
Product TV  TV  Computer    TV  TV  Computer    Computer    Computer    TV  Radio   TV  Radio   Radio   Radio   Computer    Computer    TV  TV  Radio   TV

I want to group the dataframe into something like:

Country Brazil Brazil Brazil Bolívia Bolívia Bolívia Chile Chile Chile
Order 7 3 0 25 21 22 22 55 31 58
Product Tv Computer Radio Tv Computer Radio Tv Computer Radio

How can I make it?

CodePudding user response:

You can group by the by country and product and sum by order:

df2 = df.groupby(['Country','Product'])['Order'].sum()

  •  Tags:  
  • Related