[Python] How to sum NaNs to NaN instead of zero in groupby.transform

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I need to use the transform function to sum column 'bar' when I group by column 'foo'. I use the following code

    df.groupby(['foo'])['bar'].transform(np.sum)


    However, when all the values in 'bar' are NaNs, my desired output is NaNs but the above code returns zero instead. How can I fix this? I know in the sum function I can use min_count = 1 but I am not sure how to use that in the above context.

    Continue reading...

Compartilhe esta Página