0

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

5
  • What exactly is the question here? What is the purpose of the loop? Not providing a minimal reproducible example here means essentially that you ask other people to do all the work for you. While creating such a minimal reproducible example by yourself would probably even allow you to solve the issue alone without the need to ask a question here - and in case it doesn't you have the minimal reproducible example to post here. Commented Feb 11, 2018 at 22:55
  • I'm sorry that I made it to complicated, I was just trying to explain as best as possible, I edited the question to better replicate the situation, as I said in the post, the problem is that when I run my code instead of the code ploting 1 chart per pair of index it's ploting the same chart for each row in the dataframe Commented Feb 11, 2018 at 23:11
  • Note that you do not have a Multiindex here. What you call Index is a normal column. Commented Feb 11, 2018 at 23:26
  • I know, I added it after, but it isn't working for some reason (in the original code is and I have no idea why) df.set_index(['Index1','Index2']) I mean... Commented Feb 11, 2018 at 23:32
  • Just keep it like this. An index should somehow index your data, which wouldn't be the case if there are several identical indices for different rows. Commented Feb 11, 2018 at 23:38

1 Answer 1

0

I don't see what the loop is doing, but if a loop variable is not used anywhere inside the loop it's a good sign that the loop is not even needed.

Here it seems you want to plot each group, hence

df.groupby("Index2").plot(x="X", y="Y", kind="scatter")

Complete example:

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.groupby("Index2").plot(x="X", y="Y", kind="scatter")

plt.show()

However, if you really want to loop for whatever reason, you could loop over the groupby:

for (n, group) in df.groupby("Index2"):
    group.plot.scatter(x="X", y="Y")
1
  • Well, that's exactly what I meant, I'm sorry about my question being off the regulations! I'm new to all this, but thank you! It was so simple in the end, I spent a few hours on this... Commented Feb 11, 2018 at 23:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.