3

i have a dataset that i read in with pd.read_excel

Block     Con     
  1       100      
  1       100      
  1       100      
  1       33      
  1       33       
  1       33
  2       100
  2       100
  2       100
  2       33
  2       33
  2       33
...

there are a total of 10 'block's, each 'block' has 2 types of 'con': 100 and 33. how can i iterate through the 'Block' column so that for each 'block' it prints out the 2 types of 'con': 100 and 33

desire output:

      1      100
              33
      2       100
              33

my code:

for b in data.Block:
     for c in data.Con:
         print(c)

but it prints out all of the con for each row of block.

0

2 Answers 2

2

Use drop_duplicates:

In [11]: df
Out[11]:
    Block  Con
0       1  100
1       1  100
2       1  100
3       1   33
4       1   33
5       1   33
6       2  100
7       2  100
8       2  100
9       2   33
10      2   33
11      2   33

In [12]: df.drop_duplicates()
Out[12]:
   Block  Con
0      1  100
3      1   33
6      2  100
9      2   33
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, that makes perfect sense. but my actual table is much more complicated than this, that why i was wondering how to iterate through the rows by some condition. please see my edited post.
@Jessica you should ask this as another question rather than edit the old one to change it. That way if someone else googles for your question/answer they find this :)
please see my new post here: stackoverflow.com/questions/33573329/…
0

I would use groupby in this way:

d = df.groupby(['Block','Con']).size()

which returns:

Block  Con
1      33     3
       100    3
2      33     3
       100    3

2 Comments

thank you, but my actual table is much more complicated than this, please see my edited post.
please see my new post here: stackoverflow.com/questions/33573329/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.