Home > Enterprise >  Grouping information by multiple criteria?
Grouping information by multiple criteria?

Time:04-11

so I have a data frame similar to the one below where stamp is an date time index; for context, it represents orders received and my goal is to match orders that may be the same but have come as two separate orders.

Stamp Price. indicator EX qty
1234 10 1 d 12
2345 30 -1 d 13

I want to group entries that have the same date time stamp, given that those entries have the same EX and Indicator.

I think I know how to do this with just the stamp however I'm unsure how to add the conditions of EX and indicator to the groupby.

Beginner here so any help is greatly appreciated!

CodePudding user response:

Try this:

df.groupby(["Stamp", "EX", "indicator"])

And if you then want to get the sum of quantities and prices you can do this:

df.groupby(["Stamp", "EX", "indicator"]).sum()

CodePudding user response:

you can groupby more than one column: df.groupby(['Stamp', 'EX']) Then you can check the length of each group to see if there are multiple rows that share both columns:

df.groupby(['Stamp', 'EX']).apply(len)
  • Related