I have a dataframe
that has multiple columns, containing different categories (['A'], ['1','2','3','4'])
Index1 Index2 X Y
A '1' 1 2
A '1' 5 3
A '1' 3 4
A '2' 3 1
A '2' 4 1
A '2' 3 5
A '2' 1 2
A '3' 5 3
A '3' 3 4
A '4' 3 1
A '4' 4 1
A '4' 3 5
I need to loop it so that it brings me four different splatter charts for each pair of indexes (in the future there will be a B index, that's the reason for the multiindex).
My code at the moment brings me one chart for every line (in this example would be 12 of them), if I break
at the end it brings me only one
I tried .iterows()
and .itertuples()
both of them got me the same result (maybe have been using them wrong to)
import pandas as pd
from matplotlib import pyplot as plt
Index1 = ['A','A','A','A','A','A','A','A','A','A','A','A']
Index2 = ['1','1','1','2','2','2','2','3','3','4','4','4']
X = [1,5,3,3,4,3,1,5,3,3,4,3]
Y = [2,3,4,1,1,5,2,3,4,1,1,5]
df = pd.DataFrame(Index1)
df = df.assign(Index2 = Index2,X=X,Y=Y)
df.set_index(['Index1','Index2'])
second_index = 1
for index in df.itertuples():
df = df.groupby('Index2').get_group(second_index)
df.plot.scatter(x = 'X', y = 'Y')
plt.show()
break
I have a similar code runing on a dictionary
that works on the same logic and it brings me all the charts that I need.
p.s.: that's not the real code just the general idea, and I might have made some mistakes
Multiindex
here. What you callIndex
is a normal column.df.set_index(['Index1','Index2'])
I mean...