熊猫:不要计算聚合函数中的nan

时间:2017-02-07 01:45:56

标签: python-3.x pandas aggregate aggregate-functions nan

我有以下代码:

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

然后我的结果如下:

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

我想知道怎么不在player_set中显示nana?即我希望结果数据框看起来像:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

替换

set(list(x))

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

取出nan s