How to write this GroupBy-Apply-Aggregate in one line?

Question:

Is there a better way to write this such that I don’t need to add a new column to the existing dataframe?

## Sample Data -- df_big
Groups, TotalOwned
0, 1
0, 5
1, 3
2, 2
2, 1

## My Code
df_mult = df_big[['Groups', 'TotalOwned']].copy()
df_mult['Multiple'] = np.where(df_mult['TotalOwned'] > 1, 1, 0)
df_mult.groupby('Groups')['Multiple'].mean().sort_index()

## Output -- now with fixed output!
Groups
0    0.5
1    1.0
2    0.5
Asked By: Brandon Brown

||

Answers:

your data and the result don’t match. However, just focusing on making these three lines into a single line. this is one way to accomplish it

df.assign(mult=np.where(df['TotalOwned'] > 1, 1, 0)
         ).groupby('Groups')['mult'].mean()

Result, based on the provided data and the code to be combined into a single line

Groups
0    0.5
1    1.0
2    0.5
Name: mult, dtype: float64
Answered By: Naveed

IIUC you can do in one-line:

df_out = df.assign(TotalOwned=(df['TotalOwned'] > 1)).groupby('Groups').mean()
Answered By: SomeDude
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.