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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Jessica
  • 2,923
  • 8
  • 25
  • 46

2 Answers2

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
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • 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 Nov 06 '15 at 17:46
  • @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 :) – Andy Hayden Nov 06 '15 at 18:01
  • please see my new post here: http://stackoverflow.com/questions/33573329/iterate-through-rows-on-condition-pandas-python – Jessica Nov 06 '15 at 18:31
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
Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
  • thank you, but my actual table is much more complicated than this, please see my edited post. – Jessica Nov 06 '15 at 17:45
  • please see my new post here: http://stackoverflow.com/questions/33573329/iterate-through-rows-on-condition-pandas-python – Jessica Nov 06 '15 at 18:32