简体   繁体   中英

Plot with pandas: group and mean

My data from my 'combos' data frame looks like this:

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]

I plot the results like this:

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')

And I get this plot:

在此处输入图片说明

How can I plot means of "pred" for each "lmi" data point when keeping the pairs (lmi, pr) intact?

As of your updates to the question it is now clear that you want to calculate the means for each pair (pr, lmi) . This can be done by grouping over these columns and then simply calling mean() . With reset_index() , we then restore the DataFrame format to the previous form.

$ 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

In this new DataFrame pred does contain the means and you can use the same plotting procedure you have been using before.

You can first group your DataFrame by lmi then compute the mean for each group just as your title suggests:

combos.groupby('lmi').pred.mean().plot()

In one line we:

  1. Group the combos DataFrame by the lmi column
  2. Get the pred column for each lmi
  3. Compute the mean across the pred column for each lmi group
  4. Plot the mean for each lmi group

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM