Home > OS >  How can I iterate over a numpy multidimensional array of dataframes?
How can I iterate over a numpy multidimensional array of dataframes?

Time:06-01

Im trying to iterate over a multidimensional array on Python but I'm having problems because my array is full of dataframes instead of int().

I have a multidimensional numpy array (12, 11) which contains 12 x 11 different dataframes.

nombres_df =            np.array([[df_01_ID4034, df_02_ID4034, df_03_ID4034, df_04_ID4034, df_05_ID4034, df_06_ID4034, df_07_ID4034, df_08_ID4034, df_09_ID4034, df_10_ID4034, df_11_ID4034, df_12_ID4034], 
                         [df_01_ID4035, df_02_ID4035, df_03_ID4035, df_04_ID4035, df_05_ID4035, df_06_ID4035, df_07_ID4035, df_08_ID4035, df_09_ID4035, df_10_ID4035, df_11_ID4035, df_12_ID4035], 
                         [df_01_ID4039, df_02_ID4039, df_03_ID4039, df_04_ID4039, df_05_ID4039, df_06_ID4039, df_07_ID4039, df_08_ID4039, df_09_ID4039, df_10_ID4039, df_11_ID4039, df_12_ID4039], 
                         [df_01_ID4040, df_02_ID4040, df_03_ID4040, df_04_ID4040, df_05_ID4040, df_06_ID4040, df_07_ID4040, df_08_ID4040, df_09_ID4040, df_10_ID4040, df_11_ID4040, df_12_ID4040],
                         [df_01_ID4041, df_02_ID4041, df_03_ID4041, df_04_ID4041, df_05_ID4041, df_06_ID4041, df_07_ID4041, df_08_ID4041, df_09_ID4041, df_10_ID4041, df_11_ID4041, df_12_ID4041],
                         [df_01_ID4042, df_02_ID4042, df_03_ID4042, df_04_ID4042, df_05_ID4042, df_06_ID4042, df_07_ID4042, df_08_ID4042, df_09_ID4042, df_10_ID4042, df_11_ID4042, df_12_ID4042],
                         [df_01_ID4047, df_02_ID4047, df_03_ID4047, df_04_ID4047, df_05_ID4047, df_06_ID4047, df_07_ID4047, df_08_ID4047, df_09_ID4047, df_10_ID4047, df_11_ID4047, df_12_ID4047],
                         [df_01_ID4049, df_02_ID4049, df_03_ID4049, df_04_ID4049, df_05_ID4049, df_06_ID4049, df_07_ID4049, df_08_ID4049, df_09_ID4049, df_10_ID4049, df_11_ID4049, df_12_ID4049],
                         [df_01_ID4056, df_02_ID4056, df_03_ID4056, df_04_ID4056, df_05_ID4056, df_06_ID4056, df_07_ID4056, df_08_ID4056, df_09_ID4056, df_10_ID4056, df_11_ID4056, df_12_ID4056],
                         [df_01_ID4059, df_02_ID4059, df_03_ID4059, df_04_ID4059, df_05_ID4059, df_06_ID4059, df_07_ID4059, df_08_ID4059, df_09_ID4059, df_10_ID4059, df_11_ID4059, df_12_ID4059],
                         [df_01_ID4075, df_02_ID4075, df_03_ID4075, df_04_ID4075, df_05_ID4075, df_06_ID4075, df_07_ID4075, df_08_ID4075, df_09_ID4075, df_10_ID4075, df_11_ID4075, df_12_ID4075]], dtype="object") 

for j in range(len(nombres_df)): 
    for i in range(len(nombres_df[i])):
        print (nombres_df[i][j])

I need to iterate over it and make operations with values inside each dataframe. The problem is that when I try to iterate as usually, I cannot do it because I'm getting this error:

 5 
 6 for j in range(len(nombres_df)):
 7     for i in range(len(nombres_df[i])):    <--------
 8         print (nombres_df[i][j], end = " ")

IndexError: arrays used as indices must be of integer (or boolean) type

I know the problem is here len(nombres_df[i]) but I don`t know how to solve it.

Thank you very much

CodePudding user response:

I thick the problem is the fact that you are iterating over the wrong index in the second line of your code. that i inside range(len(nombres_df[i])) shoud be j

also you inverted the indexes in nombres_df[i][j] it shoud be nombres_df[j][i]

this shoud do the trick

for j in range(len(nombres_df)): 
    for i in range(len(nombres_df[j])):
        print (nombres_df[j][i])
  • Related