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
df['col3'].add(10)? Why groupby?Seriesof the original shape of the DataFrame, so the output is aSeriesindexed 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 betweengroupby.sum()andgroupby.cumsum()as_index=False?