0

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
5
  • Are you sure you want a group by here? Commented Sep 3, 2019 at 14:40
  • May be just df['col3'].add(10)? Why groupby? Commented Sep 3, 2019 at 14:42
  • It doesn't make sense to have the group keys output. Your calculation returns a Series of the original shape of the DataFrame, so the output is a Series indexed like the original DataFrame. Your output would be indexed with the group keys when there is an aggregation that gives you one value per group. I.e. the difference between groupby.sum() and groupby.cumsum() Commented Sep 3, 2019 at 14:44
  • 1
    It is common output for apply , adding additional index Commented Sep 3, 2019 at 14:45
  • ......deleting as_index=False ? Commented Sep 3, 2019 at 14:51

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.