Edamame
Edamame

Reputation: 25376

pandas: do not count nan in an aggregate function

I have the following code:

data_agg_df = data_df.groupby("team", as_index=False).player.agg({"player_set": lambda x: set(list(x)), "player_count": "nunique"})

Then my results look like:

team           player_set         player_count
-------------------------------------------------
 A           {John, Mary}          2
 B           {nan}                 0
 C           {Dave,nan}            1

I am wondering how to not show the nana in the player_set? i.e. I want the resulting data frame look like:

team           player_set         player_count
-------------------------------------------------
 A           {John, Mary}          2
 B           {}                    0
 C           {Dave}                1

Thanks!

Upvotes: 0

Views: 273

Answers (1)

Haleemur Ali
Haleemur Ali

Reputation: 28313

replace

set(list(x))

with

set(list(i for i in x if pd.notnull(i)))

to take out the nans

Upvotes: 1

Related Questions