I have a scenario where I have to group a dataframe by a column and again group the resulting dataframe using a different column. The reason I am iteratively grouping is because its easier for me to sack the results to a different dataframe. So the way I am trying to do is
g=df.groupby(col1):
for a,b in g:
for c,d in b.groupby(col2):
When I try to do the above I am getting a None value in c instead of value associated with col2. What am I doing incorrectly?
CodePudding user response:
We can't know with what you've given us, but your code should work fine... it does for me.
df = pd.DataFrame({'EmployeeNo':[11111,11112,11113,11115,11116,11128],
'OutletName':['Outlet1', 'Outlet2', 'Outlet3','Outlet4', 'Outlet5','Outlet6'],
'EmployeeName':['John','Tom','Bob','Sam', 'Sean', 'Zac'],
'TargetAmount':[1000,500,400,500,300,800]})
g=df.groupby('OutletName')
for a,b in g:
for c,d in b.groupby('EmployeeName'):
print(c)
Output:
John
Tom
Bob
Sam
Sean
Zac