[英]Python Pivot Table KeyError
I have a pivot table that I looking to get values from and add to a new column on a separate data frame.我有一个 pivot 表,我希望从中获取值并将其添加到单独数据框的新列中。 I'd like to add the 'ReleaseSpeed' value to the new dataframe based on the 'PitcherID' matching on both the pivot table and dataframe, for the given 'PitchType'... however, I'm having issues getting the code to call upon the pitch type.
我想在新的Z6A8064B5DF479455555555555555555555555057DZ上添加“发行”值调用音高类型。
To get the pivot table I used: pitcher_avg = pitcher_avg.pivot_table(index = ['PitcherID'], columns = ['PitchType'], values = ['ReleaseSpeed'], aggfunc = np.mean, fill_value = 0).reset_index()为了获得 pivot 表,我使用了: pitcher_avg = pitcher_avg.pivot_table(index = ['PitcherID'], columns = ['PitchType'], values = ['ReleaseSpeed'], aggfunc = np.mean, fill_value = 0).reset_index ()
PitcherID ReleaseSpeed ...
PitchType CB CF ... SF SI SL
0 80027 0.000000 86.022476 ... 0.000000 86.953833 80.533818
1 113724 0.000000 85.923250 ... 0.000000 89.452660 77.514283
2 142254 0.000000 0.000000 ... 0.000000 93.813669 86.085831
3 145462 75.401915 86.017263 ... 83.681604 0.000000 0.000000
4 149319 83.115615 93.617160 ... 0.000000 95.678535 0.000000
.. ... ... ... ... ... ... ...
868 774828 0.000000 0.000000 ... 0.000000 92.273510 84.243239
869 775376 76.968184 0.000000 ... 0.000000 0.000000 87.667449
870 783719 76.871411 0.000000 ... 85.757180 90.571193 83.681105
871 796795 73.575867 0.000000 ... 83.693867 0.000000 0.000000
872 796926 59.545178 0.000000 ... 0.000000 79.432177 70.142013
I've tried我试过了
FB_same_id_conditions = [total_data['PitcherID'] == pitcher_avg['PitcherID'] & pitcher_avg['PitchType'] == 'FB']... but this returns KeyError: 'PitchType' FB_same_id_conditions = [total_data['PitcherID'] == pitcher_avg['PitcherID'] & pitcher_avg['PitchType'] == 'FB'] ...但这会返回 KeyError: 'PitchType'
I've also tried我也试过
total_data['CBAvgVelo'] = np.where(pitcher_avg['PitchType'],= 'FB' & total_data['PitcherID'] == pitcher_avg['PitcherID'],"",pitcher_avg['ReleaseSpeed']) total_data['CBAvgVelo'] = np.where(pitcher_avg['PitchType'],= 'FB' & total_data['PitcherID'] == pitcher_avg['PitcherID'],"",pitcher_avg['ReleaseSpeed'])
The desired output is something like this:所需的 output 是这样的:
PitcherID CBAvgVelo CFAvgVelo
0 80027 0.000000 86.022476
1 80027 0.000000 86.022476
2 145462 75.401915 86.017263
3 145462 75.401915 86.017263
4 145462 75.401915 86.017263
You can import & use pandas for it.您可以为此导入和使用 pandas。 it will easily convert from one dateframe to another dataframe.
它将轻松地从一个日期帧转换为另一个 dataframe。 you can refer [doc1][1] & [doc2][2] & [doc3][3].
您可以参考 [doc1][1] & [doc2][2] & [doc3][3]。
pivoted = df.pivot(index='PitcherID', columns='CBAvgVelo', values='CFAvgVelo')\
.reset_index()
pivoted.columns.name=None
print(pivoted)
# PitcherID CBAvgVelo CFAvgVelo
#0 80027 0.000000 86.022476
#1 80027 0.000000 86.022476 ```
Hope this helps...
[1]: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html
[2]: https://pythoninoffice.com/get-values-rows-and-columns-in-pandas-dataframe/#:~:text=pandas%20get%20rows%20We%20can%20use.loc%20%5B%5D%20to,left%20blank%2C%20we%20can%20get%20the%20entire%20row.
[3]: https://www.geeksforgeeks.org/python-pandas-dataframe/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.