Reputation: 487
So I am using the groupby function in pandas to get mean of two columns using conditions based on two other columns. I am having trouble creating the matplotlib plots
An example table is
data_temp = pd.DataFrame([
[3, 16, 0, 0, 10],
[3, 20, 0, 1, 11],
[3, 25, 0, 2, 11],
[3, 30, 0, 3, 15],
[4, 30, 0, 0, 0],
[4, 45, 0, 1, 0],
[4, 54, 0, 2, 0],
[4, 54, 0, 3, 0],
[5, 31, 0, 0, 14],
[5, 32, 0, 1, 15],
[5, 45, 0, 2, 0],
[5, 46, 0, 3, 0],
[3, 1, 0, 0, 11],
[3, 5, 0, 1, 12],
[3, 6, 0, 2, 13],
[3, 8, 0, 3, 11],
[4, 35, 0, 0, 0],
[4, 25, 0, 1, 0],
[4, 34, 0, 2, 0],
[4, 24, 0, 3, 0]
], columns=list('ABCDE'))
result = data_temp.groupby(['A', 'D']).agg({'B':'mean', 'E':'mean'})
print(result)
I get
B E
A D
3 0 8.5 10.5
1 12.5 11.5
2 15.5 12.0
3 19.0 13.0
4 0 32.5 0.0
1 35.0 0.0
2 44.0 0.0
3 39.0 0.0
5 0 31.0 14.0
1 32.0 15.0
2 45.0 0.0
3 46.0 0.0
Now I am trying to plot the data where x axis = A y axis = B mean and I have 4 plots one for each D value
Similarly a plot for E mean on a separate plot
I tried a couple of things but the main issue I face is groupby creates a hash table like structure.
Upvotes: 2
Views: 902
Reputation: 294536
Use unstack
on result:
result2 = result.unstack()
reuslt2
Then B.plot()
result2.B.plot()
And E.plot()
result2.E.plot()
Upvotes: 1
Reputation: 2314
How about something like this:
import matplotlib.pyplot as pp
data = A.groupby(['D','A'])['E','B'].mean().reset_index()
#For the plot of B vs A
fig, ax = pp.subplots()
for value, groups in data.groupby('D'):
ax.plot(group.A,group.B)
pp.show()
Upvotes: 0