Reputation: 259
df = pd.DataFrame({'order':['A', 'B', 'C', 'D', 'E', 'F'],'quantity':[1,1,2,3,3,4]})
df_out = df.order.repeat(df.quantity).reset_index(drop=True).to_frame()
df_out['grp'] = df_out.index // 4
df_out.groupby(['grp','order'])['order'].count().to_frame(name='quantity')
output :
quantity
grp order
0 A 1
B 1
C 2
1 D 3
E 1
2 E 2
F 2
3 F 2
In groupby() function I have got my desired result.
But when i try to Concat()
with df1
,
df1 = pd.DataFrame({'order':['A', 'B', 'C', 'D', 'E', 'F'],'quantity':[1,1,2,3,3,4]})
I found that 0
from grp
is assigned to only first row
grp order
0 A 1
not as
quantity
grp order
0 A 1
0 B 1
0 C 2
How can I solve this problem ?
Upvotes: 0
Views: 31
Reputation:
What you get after groupby(*multiple_columns*).*some_action*
is a Dataframe
with MultiIndex
. You can reset it:
ans = (
df_out
.groupby(['grp', 'order'])['order']
.count()
.to_frame(name='quantity')
.reset_index())
Then you may use any column as index and drop that column:
ans.index = ans['grp']
ans = ans.drop('grp', axis=1)
ans
is:
order quantity
grp
0 A 1
0 B 1
0 C 2
1 D 3
1 E 1
2 E 2
2 F 2
3 F 2
Upvotes: 1