Python 与熊猫的情节:分组和平均值
“组合”数据框中的数据如下所示:Python 与熊猫的情节:分组和平均值,python,pandas,plot,Python,Pandas,Plot,“组合”数据框中的数据如下所示: pr = [1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,.....1.0,2.0,3.0,4.0] lmi = [200, 200, 200, 250, 250,.....780, 780, 780, 800, 800, 800] pred = [0.16, 0.18, 0.25, 0.43, 0.54......., 0.20, 0.34, 0.45, 0.66] fig,ax = plt.subplo
pr = [1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,.....1.0,2.0,3.0,4.0]
lmi = [200, 200, 200, 250, 250,.....780, 780, 780, 800, 800, 800]
pred = [0.16, 0.18, 0.25, 0.43, 0.54......., 0.20, 0.34, 0.45, 0.66]
fig,ax = plt.subplots()
for pr in [1.0,2.0,3.0,4.0]:
ax.plot(combos[combos.pr==pr].lmi, combos[combos.pr==pr].pred, label=pr)
ax.set_xlabel('lmi')
ax.set_ylabel('pred')
ax.legend(loc='best')
我将结果绘制如下:
pr = [1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,1.0,2.0,3.0,4.0,.....1.0,2.0,3.0,4.0]
lmi = [200, 200, 200, 250, 250,.....780, 780, 780, 800, 800, 800]
pred = [0.16, 0.18, 0.25, 0.43, 0.54......., 0.20, 0.34, 0.45, 0.66]
fig,ax = plt.subplots()
for pr in [1.0,2.0,3.0,4.0]:
ax.plot(combos[combos.pr==pr].lmi, combos[combos.pr==pr].pred, label=pr)
ax.set_xlabel('lmi')
ax.set_ylabel('pred')
ax.legend(loc='best')
我得到了这个情节:
在保持成对(lmi,pr)完整的情况下,如何为每个“lmi”数据点绘制“pred”的平均值?您可以首先根据
lmi
对数据帧进行分组,然后计算各组的平均值,正如标题所示:
combos.groupby('lmi').pred.mean().plot()
在一行中,我们:
lmi
列对组合DataFrame
进行分组lmi的pred
列
lmi
组的pred列的平均值lmi
组的平均值根据您对问题的更新,现在很清楚您想要计算每对
(pr,lmi)
的平均值。这可以通过在这些列上分组,然后简单地调用mean()
来实现。使用reset\u index()
,然后将数据帧格式恢复为以前的格式
$ combos.groupby(['lmi', 'pr']).mean().reset_index()
lmi pr pred
0 200 1.0 0.16
1 200 2.0 0.18
2 200 3.0 0.25
3 250 1.0 0.54
4 250 4.0 0.43
5 780 2.0 0.20
6 780 3.0 0.34
7 780 4.0 0.45
8 800 1.0 0.66
在这个新的数据框中,pred确实包含了方法,您可以使用以前使用过的相同的绘图过程