Reputation: 1
I'm looking to use values from a dataframe generated through Groupby()
. The command I wrote was:
pitcher_avg = total_data.groupby(['PitcherID','PitchType'])['ReleaseSpeed'].mean()
The pitcher_avg
output was:
PitcherID PitchType
80027 CF 86.022476
CH 80.846935
FB 89.614138
SI 86.953833
SL 80.533818
My next step is taking the values in the right-hand column, and merging them into a new dataframe on the condition that the PitcherID matched the PitcherID in the new dataframe. For example:
GameDate PitcherID ... CF CH
0 2018-03-29 80027 ... 86.022476 80.846935
1 2018-03-29 80027 ... 86.022476 80.846935
2 2018-03-29 80027 ... 86.022476 80.846935
3 2018-03-29 80027 ... 86.022476 80.846935
4 2018-03-29 80027 ... 86.022476 80.846935
Is this possible? I've been trying np.where
and pd.merge
commands, but nothing has worked yet. Thank you!
Upvotes: 0
Views: 68
Reputation: 451
I think you should do .reset_index()
and rename new column after the groupy by. Merge should work after that. You can specify in merge on which columns to merge i.e.
df_original.merge(grouped_results_df, how ='left', indicator =True, on = ['PitcherID','PitcherType','NewColumn'])
Upvotes: 1