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
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
IIUC you can do in one-line:
df_out = df.assign(TotalOwned=(df['TotalOwned'] > 1)).groupby('Groups').mean()
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
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
IIUC you can do in one-line:
df_out = df.assign(TotalOwned=(df['TotalOwned'] > 1)).groupby('Groups').mean()