小编bke*_*sey的帖子

如何更改 pandas groupby.agg 函数的输入参数?

我在使用 groupby_object.agg() 方法和想要更改输入参数的函数时遇到问题。是否有可用的函数名称资源 .agg() 接受,以及如何将参数传递给它们?

请参阅下面的示例:

import pandas as pd
import numpy as np

df = pd.DataFrame({'numbers': [1, 2, 3, 2, 1, 3], 
               'colors': ['red', 'white', 'blue', 'red', 'white', np.nan], 
               'weight': [10, 10, 20, 5, 10, 20]})

df['colors'].nunique() # Returns 3 as NaN is not counted
df['colors'].nunique(dropna=False) # Returns 4 as NaN is counted
Run Code Online (Sandbox Code Playgroud)

当我“颜色”时,groupby如何dropna=False通过函数传递参数?

df.groupby('numbers').agg({'colors': 'nunique', 'weight': 'sum'})
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

2
推荐指数
1
解决办法
712
查看次数

标签 统计

pandas ×1

pandas-groupby ×1

python ×1