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?