I have a dataframe which looks like this:
dfB
name value country
benzene spice Australia
benzene spice Australia
benzene spice Australia
benzene herbs Australia
benzene herbs Americas
benzene anise Poland
methyl herbs
methyl herbs Americas
methyl spice Americas
alcohol spice Germany
alcohol spice Germany
I want to createa a different dataframe which is an aggregation of the country column, something like this:
dfB
name value country count
benzene spice Australia 3
benzene herbs Australia 1
benzene herbs Americas 1
benzene anise Poland 1
methyl herbs 1
methyl herbs Americas 1
methyl spice Americas 1
alcohol spice Germany 2
The idea is to aggregare the country column and create a count for the country column for each unique "name" and "value" combination. If there is a blank or Nan it should also he treated differently.
I tried using groupby:
grouped = dfB.groupby(["name", "value", "country"]).agg({"country": "count"})
but it does not seem to create the dataframe how I intend to. How can I do this?