Pandas Panel "iterrows" (iteration along the major axis)

I have a panel pandasthat looks like this:

import pandas as pd
import numpy as np
P = pd.Panel(np.arange(90).reshape(5,6,3))

I want to create a generator along the main axis, similar pd.DataFrame.iterrows, so that with each call it nextreturns a tuple containing the current index of the main axis and the pd.DataFrameshape (P.shape[0], P.shape[2]). pandasIs this feature already available in ? If not, can I use tetra pd.DataFrame.iterrowsor numpyto efficiently replicate this feature?

Thank.

+4
source share
2 answers

I would do this:

for m, df in P.to_frame().unstack().iterrows():
    print m
    print df.unstack()

Better:

for i, item in P.swapaxes(0, 1).iteritems():
    print i
    print item

Decision

P.swapaxes(0, 1).iteritems()
+2
source

- Is this what you want?

In [176]: [i for i in P.iteritems()]
Out[176]:
[(0,     0   1   2
  0   0   1   2
  1   3   4   5
  2   6   7   8
  3   9  10  11
  4  12  13  14
  5  15  16  17), (1,     0   1   2
  0  18  19  20
  1  21  22  23
  2  24  25  26
  3  27  28  29
  4  30  31  32
  5  33  34  35), (2,     0   1   2
  0  36  37  38
  1  39  40  41
  2  42  43  44
  3  45  46  47
  4  48  49  50
  5  51  52  53), (3,     0   1   2
  0  54  55  56
  1  57  58  59
  2  60  61  62
  3  63  64  65
  4  66  67  68
  5  69  70  71), (4,     0   1   2
  0  72  73  74
  1  75  76  77
  2  78  79  80
  3  81  82  83
  4  84  85  86
  5  87  88  89)]

or

In [174]: [print(i[1]) for i in P.iteritems()]
    0   1   2
0   0   1   2
1   3   4   5
2   6   7   8
3   9  10  11
4  12  13  14
5  15  16  17
    0   1   2
0  18  19  20
1  21  22  23
2  24  25  26
3  27  28  29
4  30  31  32
5  33  34  35
    0   1   2
0  36  37  38
1  39  40  41
2  42  43  44
3  45  46  47
4  48  49  50
5  51  52  53
    0   1   2
0  54  55  56
1  57  58  59
2  60  61  62
3  63  64  65
4  66  67  68
5  69  70  71
    0   1   2
0  72  73  74
1  75  76  77
2  78  79  80
3  81  82  83
4  84  85  86
5  87  88  89
Out[174]: [None, None, None, None, None]
+2
source

Source: https://habr.com/ru/post/1648155/


All Articles