I have the following data similar to the below:
Table 1
Colour Make
Red Ford
Blue BMW
Blue BMW
Green Golf
Yellow Audi
Yellow Audi
Yellow Audi
Table 2
Colour Make Count
Green Ford 5
Blue BMW 1
Green Golf 6
Orange BMW 1
I would like to use pandas to aggregate the data in table 1, then either increment the count in table 2 if it already exists, or insert a new record if it does not exist. From the example data above:
Resultant table:
Colour Make Count
Green Ford 5
Blue BMW 3
Green Golf 7
Orange BMW 1
Red Ford 1
Yellow Audi 3
To complete the first aggregation step, I have:
df1.groupby(["Colour", "Make"]).size()reset_index(name="Count")
However, I'm not sure how to approach the second step. I'm inclined to opt for some kind of loop-based solution, but I've read that this is a no-no.
What would be the most appropriate way to get to the resultant table?
Thank you in advance.