Skip to content
Advertisement

How to get unique counts based on a different column, with pandas groupby

I have the following dataframe:

JavaScript

I would like to groupby effortduration and get the count of each column based on the unique count of the user column. This is what I have tried so far:

JavaScript

However, that is again not what I am looking for because the values of callbacks and applications are not based on the user column. My result should be something like this:

JavaScript

Is there any way to do such a thing? If yes, is it also possible to generalize it because my original dataframe has many more columns and it would be painful to write all the functions by hand?

Advertisement

Answer

  • This works with the sample data, I’m not sure with real data
  • Replace 0, with NaN, and then drop NaN if 'effortduration', 'callbacks', and 'applications' are all NaN.
  • Drop all duplicates
    • Based on the desired result, it only matters if a user called/applied, once.
  • Groupby count
JavaScript

nunique

  • As already noted, this option returns the count of the number of unique values in the column, so doesn’t return the desired output.
JavaScript
Advertisement