Skip to content
Advertisement

Count value pairings from different columns in a DataFrame with Pandas

I have a df like this one:

JavaScript

df:

JavaScript

I want to transform this into a df that looks like this

JavaScript

So for every item i want a row with the possible combinations of cup and size and an additional row with the frequency.

What is the proper way to do this using pandas?

Advertisement

Answer

Let’s try:

  1. Add a frequency column to the dataframe to indicate individual rows are worth 1 each.

  2. groupby sum to get the current count in the DataFrame.

  3. Create a MultiIndex from the unique values in each column.

  4. Use the new midx to reindex with a fill_value=0 so that freq gets filled with 0 when created by the new index.

  5. reset_index to convert the index back into columns.

JavaScript

df:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement