Reputation: 383
I have a column where its categorical (house, neighbors, routine). And I have 4 extra columns. The dataset looks like this:
print(df)
type num_before_cleaning num_after_cleaning num_before_removing num_after_removing
0 house 32 12 42 10
1 house 10 3 4 1
2 neighbors 20 5 25 7
3 routine 40 21 62 35
4 neighbors 14 2 21 9
5 routine 52 30 71 42
and I want for each category in column type it will divide num_before_cleaning / num_after_cleaning
and num_before_removing / num_after_removing
So, the outcome will be for example:
print(house_cleaning)
0.64
print(routine_removing)
0.79
I know that I should use np.where
but how can I make it perform calculations after giving it a specific condition? Or is there any other ways I can solve it.
I've tried researching but didn't find any answers.
Upvotes: 0
Views: 72
Reputation: 863166
I believe you need:
df1 = df.groupby('type').sum()
df1 = df1.assign(clean = df1.pop('num_before_cleaning').div(df1.pop('num_after_cleaning')),
remove = df1.pop('num_before_removing').div(df1.pop('num_after_removing')))
print (df1)
clean remove
type
house 2.800000 4.181818
neighbors 4.857143 2.875000
routine 1.803922 1.727273
Upvotes: 1