简体   繁体   English

Pandas groupby().apply() - 从应用函数返回 None 会弄乱结果

[英]Pandas groupby().apply() - returning None from the applied function messes up the results

I perform groupby() and apply() on a few data frames with the same structure:我在几个具有相同结构的数据帧上执行 groupby() 和 apply() :

d = d.groupby( 'groupby_col', as_index = False ).apply( some_function )

For some it works as expected, for some it fails.对于某些它按预期工作,对于某些它失败。 The way it fails is that the dataframe becomes a series where each element contains just column names.它失败的方式是数据框变成了一个系列,其中每个元素只包含列名。 It looks like this:它看起来像这样:

In [18]: d.head()
Out[18]:
groupby_col
134663372801          some_col_1 some_col_2 some_col_3 some_col_4...
134663372802          some_col_1  some_col_2  some_col_3  some_col_4...
134663372803          some_col_1  some_col_2  some_col_3  some_col_4...
134663372804          some_col_1  some_col_2  some_col_3  some_col_4...
134663372805          some_col_1  some_col_2  some_col_3  some_col_4...
dtype: object

BTW, the applied function returns either a data frame with a correct number of columns or None.顺便说一句,应用函数返回具有正确列数或无的数据框。

What might be the reason for this and how to debug it?这可能是什么原因以及如何调试它?

The problem goes away if instead of returning None from the applied function I alway return a frame - replaced如果我总是返回一个框架而不是从应用的函数中返回 None ,问题就会消失 - 替换

if some_condition:
    return

with

if some_condition:
    return d[:0]        # return the empty frame so that the columns match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM