Building Pandas groupsby using subtitles and loops

I am trying to create a subtitle grid based on a Pandas groupby object. I would like each chart to be based on two columns of data for one group of a groupby object. Fake Dataset:

C1,C2,C3,C4 1,12,125,25 2,13,25,25 3,15,98,25 4,12,77,25 5,15,889,25 6,13,56,25 7,12,256,25 8,12,158,25 9,13,158,25 10,15,1366,25 

I tried the following code:

 import pandas as pd import csv import matplotlib as mpl import matplotlib.pyplot as plt import math #Path to CSV File path = "..\\fake_data.csv" #Read CSV into pandas DataFrame df = pd.read_csv(path) #GroupBy C2 grouped = df.groupby('C2') #Figure out number of rows needed for 2 column grid plot #Also accounts for odd number of plots nrows = int(math.ceil(len(grouped)/2.)) #Setup Subplots fig, axs = plt.subplots(nrows,2) for ax in axs.flatten(): for i,j in grouped: j.plot(x='C1',y='C3', ax=ax) plt.savefig("plot.png") 

But it generates 4 identical subnets with all the data plotted on each (see example below):

enter image description here

I would like to do something like the following to fix this:

 for i,j in grouped: j.plot(x='C1',y='C3',ax=axs) next(axs) 

but i get this error

AttributeError: object 'numpy.ndarray' does not have attribute 'get_figure'

I will have a dynamic number of groups in the groupby object that I want to build, and many more elements than the fake data that I provided. That's why I need an elegant dynamic solution and a data set of each group, put on a separate subplot.

+6
source share
1 answer

It looks like you want to iterate over groups and axes in parallel, so instead of having nested for loops (which repeat across all groups for each axis), you want something like this:

 for (name, df), ax in zip(grouped, axs.flat): df.plot(x='C1',y='C3', ax=ax) 

enter image description here

You have the right idea in the second code snippet, but you get an error because axs is an array of axes, but plot expects only one axis. Therefore, you should also replace next(axs) in your example with ax = axs.next() and change the plot argument to ax=ax .

+9
source

Source: https://habr.com/ru/post/987685/


All Articles