Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 忽略字典“count\u if=1”的聚合。_Python_Pandas - Fatal编程技术网

Python 忽略字典“count\u if=1”的聚合。

Python 忽略字典“count\u if=1”的聚合。,python,pandas,Python,Pandas,如果一个组的所有值都是NaN,我想使用sum聚合一个Pandas数据帧并获得NaN。这适用于.agg('sum',min_count=1)的情况,但在使用聚合字典时忽略min_count 我在这里遗漏了什么?我如何修复它 例子: sum对于所有NaN的组将返回0.0,但我们可以通过使用参数minu count=1来解决这个问题: print(df.groupby('l')['v'].agg('sum')) > l > a -1.0 > b 2.0 > c

如果一个组的所有值都是
NaN
,我想使用
sum
聚合一个Pandas数据帧并获得
NaN
。这适用于
.agg('sum',min_count=1)
的情况,但在使用聚合字典时忽略
min_count

我在这里遗漏了什么?我如何修复它

例子:
sum
对于所有
NaN
的组将返回
0.0
,但我们可以通过使用参数
minu count=1
来解决这个问题:

print(df.groupby('l')['v'].agg('sum'))
> l
> a   -1.0
> b    2.0
> c    0.0
> Name: v, dtype: float64

print(df.groupby('l')['v'].agg('sum', min_count=1))
> l
> a   -1.0
> b    2.0
> c    NaN
> Name: v, dtype: float64
但是当使用字典时,它似乎忽略了关键字参数

df.groupby('l').agg({'v':'sum', 'w':'mean'}, min_count=1)
> Name: v, dtype: float64
>      v    w
> l          
> a -1.0 -1.0
> b  2.0  1.0
> c  0.0  NaN

谢谢你的帮助

您可以使用lambda函数:

df1 = df.groupby('l').agg({'v': lambda x: x.sum(min_count=1), 'w': 'mean'})
print (df1)
     v    w
l          
a -1.0 -1.0
b  2.0  1.0
c  NaN  NaN
df1 = df.groupby('l').agg({'v': lambda x: x.sum(min_count=1), 'w': 'mean'})
print (df1)
     v    w
l          
a -1.0 -1.0
b  2.0  1.0
c  NaN  NaN