Skip to content
Advertisement

How to obtain dataframe from grouped element after using apply

Let’s say this the dataframe:

JavaScript

Then the goal is to produce this:

JavaScript

The total Val1 is Y as long as one of the instances is Y.

My code looks like this:

JavaScript

This works except that cumulative has dtype object and I can only access Val1, that is, I cannot access First Name or Last Name (Although when I run print(cumulative), it does print everything).

If I try:

JavaScript

then, I just get the column with Y or N, but not the names.

How to fix this? Moreover, can I return two arguments? one for Val1 and one for Total? or would I have to run another apply for Total and append the column to the dataframe?

Advertisement

Answer

Another way is to use groupby.agg where you use max to get “Y” if it exists (because Y>N) and count:

JavaScript

Output:

JavaScript

You can pass in a lambda that selects based whatever criteria you want. For example, the following aggregates “Val1” based on whether the number of “Y”s are greater than the number of “N”s (if there are more “Y”s select “Y” else “N”):

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