There is a pivot table enter image description here

you need to get the column "OUT" (part of the index) as values ​​for further transmission to the neuron say.

df_norm_.index 

gives a multi-index, but how to take a second column from it - I’m not going to attach my mind.

 df_norm_.index[1] 

gives a pair of indexes with the number 1: (390, 1). And the appeal

 df_norm_.index["OUT"] 

gives an error ... I certainly can twist my arms like this: enter image description here but for some reason it does not give the correct answer, too ... After all, on the previous slide 8 units in a row ... and here in fifth place is already zero ...

  • and what, df["OUT"].values will not solve the problem? - strawdog
  • @strawdog - KeyErorr in the pivot table there is no column "OUT" - this is part of the index - Vasyl Kolomiets
  • 2
    @VasylKolomiets is not suitable pandas.pydata.org/pandas-docs/stable/reference/api/… ? - Andrey
  • @Andrey, if you issue your comment as an answer - I will remove the option with df.index.get_level_values(1) from my answer ... - MaxU 1:14 pm
  • @MaxU everything is OK, do not delete. I will not issue a separate answer. - Andrey

1 answer 1

Source DF:

 In [203]: df Out[203]: 0 1 2 3 first second bar one 0.205973 -0.403744 -0.716060 0.449733 two -1.074096 1.341120 -0.216712 0.957400 baz one -0.553551 -0.624259 -1.241866 2.492807 two 1.335034 -0.456473 1.500064 1.526427 foo one 0.173070 0.567775 -0.222740 0.030894 two -0.043697 0.536522 -1.814972 -0.188887 qux one 0.269183 -1.868055 1.344501 -0.506121 two 0.498302 -0.306272 -0.293913 1.550167 

The most idiomatic variant has already been indicated by @Andrey in the comment :

 In [204]: df.index.get_level_values(1) Out[204]: Index(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype='object', name='second') 

Option 1 with .reset_index(level=<index_level_number>) :

 In [205]: df.reset_index(level=1)['second'].values Out[205]: array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype=object) 

Option 2 with .reset_index(level=<index_level_name>) :

 In [206]: df.reset_index(level='second')['second'].values Out[206]: array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype=object) 

Data to create the original toy DF:

 df = pd.DataFrame( {0: {('bar', 'one'): 0.20597346900510116, ('bar', 'two'): -1.0740962720471594, ('baz', 'one'): -0.5535505995153884, ('baz', 'two'): 1.3350343617931544, ('foo', 'one'): 0.1730697501809625, ('foo', 'two'): -0.043697458626714, ('qux', 'one'): 0.2691829986873178, ('qux', 'two'): 0.4983017887971038}, 1: {('bar', 'one'): -0.4037441703092006, ('bar', 'two'): 1.3411199938414102, ('baz', 'one'): -0.6242586374458912, ('baz', 'two'): -0.45647251856573634, ('foo', 'one'): 0.5677751470294398, ('foo', 'two'): 0.5365220610310172, ('qux', 'one'): -1.8680546696869031, ('qux', 'two'): -0.3062719539989547}, 2: {('bar', 'one'): -0.7160603640668033, ('bar', 'two'): -0.2167120301102546, ('baz', 'one'): -1.2418655822812408, ('baz', 'two'): 1.5000638406131677, ('foo', 'one'): -0.22274008778504206, ('foo', 'two'): -1.8149720525211626, ('qux', 'one'): 1.3445013025101284, ('qux', 'two'): -0.2939133657145476}, 3: {('bar', 'one'): 0.4497325390675965, ('bar', 'two'): 0.957399653216663, ('baz', 'one'): 2.492807277698116, ('baz', 'two'): 1.5264272939729429, ('foo', 'one'): 0.030894287007498675, ('foo', 'two'): -0.18888706751843662, ('qux', 'one'): -0.5061207998701763, ('qux', 'two'): 1.5501666900299136}})