0

I use groupby and apply a lot in my programs. However, I encountered an error I could not really understand: My apply goes through ALL of my pandas-groupby groups twice.

I was able to produce kind of a minimum working example of the problem:

import pandas as pd
a=pd.DataFrame({"id":[1,1,1,2,2,2,3,3,3],"value1":[1,2,3,4,5,6,7,8,9],"value2":[9,8,7,6,5,4,3,2,1]})
a.groupby("id").apply(lambda x: print(x.name))

This produces and error as the code seems to generate an additional group. While the command

a.groupby("id")["value1"].apply(lambda x: print(x.name))

works fine, the command

a.groupby("id")[["value1","value2"]].apply(lambda x: print(x.name))

does not. I am confused. It does not seem to be the "first group twice" issue.

Can somebody explain to me what is going on here?

Jan
  • 1
  • 2
  • @jezrael The OP states 'It does not seem to be the "first group twice" issue.'. Also, the suggested duplicate has an updated (non-accepted) answer that duplicate evaluation of apply is no longer done in newer versions of Pandas. – 9769953 Sep 07 '20 at 07:56
  • @00 - agree, sorry, reopeend. – jezrael Sep 07 '20 at 07:57
  • @Jan I can't reproduce your problem: I get `1 2 3 Empty Dataframe` (all newline separated) as output. What is your Pandas version? – 9769953 Sep 07 '20 at 07:58
  • I checked and I was using an old version of pandas. I updated to the current version and it worked! – Jan Sep 07 '20 at 08:53

0 Answers0