Reputation: 1671
I have a DataFrame and i want to do a "groupby-apply" style operation, however, the "key" columns were missing after the operation:
df = pd.DataFrame({
'col1':['A1', 'A2', 'A3'],
'col2':['B1', 'B1', 'B2'],
'col3':[1, 2, 3,]
})
b1 = df.groupby(['col1', 'col2'], as_index=False)[['col3']].apply(lambda x: x+10)
b2 = df.groupby(['col1', 'col2'], as_index=False)['col3'].apply(lambda x: x+10)
print(b1)
print(b2)
b1 will print as:
col3
0 11
1 12
2 13
b2 will print as:
b
0 0 11
1 1 12
2 2 13
Name: col3, dtype: int64
how can i make use the group-key columns(['col1','col2'] also be printed, as
col1 col2
A1 B1 11
A2 B1 12
A3 B2 13
Upvotes: 0
Views: 675
Reputation: 323326
When you print the type of each groupby
object
you will see , that is Series
, which will have the index ahead of it . Github Open issue
df.groupby(['col1', 'col2'], as_index=False)['col3'].apply(lambda x: type(x) )
Out[11]:
col1 col2
A1 B1 <class 'pandas.core.series.Series'>
A2 B1 <class 'pandas.core.series.Series'>
A3 B2 <class 'pandas.core.series.Series'>
dtype: object
To get the expected output
df.groupby(['col1', 'col2']).apply(lambda x: x['col3']+10).reset_index(level=-1,drop=True)
Out[32]:
col1 col2
A1 B1 11
A2 B1 12
A3 B2 13
Name: col3, dtype: int64
Upvotes: 1