0

If I have a data frame df (indexed by integer)

        BBG.KABN.S  BBG.TKA.S   BBG.CON.S   BBG.ISAT.S
index               
0       -0.004881   0.008011    0.007047    -0.000307
1       -0.004881   0.008011    0.007047    -0.000307
2       -0.005821   -0.016792   -0.016111   0.001028
3       0.000588    0.019169    -0.000307   -0.001832
4       0.007468    -0.011277   -0.003273   0.004355

and I want to iterate though each element individually (by row and column) I know I need to use .iloc(row,column) but do I need to create 2 for loops (one for row and one for column) and how I would do that?

I guess it would be something like:

for col in rollReturnRandomDf.keys():    
    for row in rollReturnRandomDf.iterrows():

       item = df.iloc(col,row)

But I am unsure of the exact syntax.

halfer
  • 19,824
  • 17
  • 99
  • 186
Stacey
  • 4,825
  • 17
  • 58
  • 99
  • 1
    Normally the best answer to "how do I iterate?" is "don't iterate!" Always use vectorized operations over iteration if possible, e.g. if you want to add 1 to every element, just do `df+1`. Exactly what is it you are trying to do? – JohnE Jul 09 '15 at 01:31
  • Here's a good reference that will both show you how to use `iterrows` and why not to. ;-) http://stackoverflow.com/questions/24870953/does-iterrows-have-performance-issues The main thing to realize about `iterrows` is that it returns a series. – JohnE Jul 09 '15 at 01:40

1 Answers1

1

Maybe try using df.values.ravel().

import pandas as pd
import numpy as np

# data
# =================
df = pd.DataFrame(np.arange(25).reshape(5,5), columns='A B C D E'.split())

Out[72]: 
    A   B   C   D   E
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
4  20  21  22  23  24


# np.ravel
# =================
df.values.ravel()

Out[74]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
       21, 22, 23, 24])

for item in df.values.ravel():
    # do something with item
Jianxun Li
  • 24,004
  • 10
  • 58
  • 76