Accessing columns after applying the stack function on a pandas dataframe

jjreddick

How can I access the columns after the stack function is applied on a dataframe?

For example, if I have a dataframe such as:

df11 = pd.DataFrame(np.random.randn(5, 3), columns=['a', 'b', 'c'])

          a         b         c
0 -1.108734  0.458352 -1.567971
1  1.656508 -0.091190 -0.700334
2 -1.278772  0.034386  0.680842
3  1.133447  0.710459 -0.562747
4  0.563312 -0.346689 -0.883099

df11.stack() produces:

0  a   -1.108734
   b    0.458352
   c   -1.567971
1  a    1.656508
   b   -0.091190
   c   -0.700334
2  a   -1.278772
   b    0.034386
   c    0.680842
3  a    1.133447
   b    0.710459
   c   -0.562747
4  a    0.563312
   b   -0.346689
   c   -0.88309

However these new columns don't have a name, and I can't seem to find a way to access them.

DSM

That's because there aren't any columns; they're now MultiIndex levels on a Series:

>>> s.index
MultiIndex(levels=[[0, 1, 2, 3, 4], [u'a', u'b', u'c']],
           labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])

There are lots of ways to get at what's inside, depending on what form you need it in:

>>> s.index.get_values()
array([(0L, 'a'), (0L, 'b'), (0L, 'c'), (1L, 'a'), (1L, 'b'), (1L, 'c'),
       (2L, 'a'), (2L, 'b'), (2L, 'c'), (3L, 'a'), (3L, 'b'), (3L, 'c'),
       (4L, 'a'), (4L, 'b'), (4L, 'c')], dtype=object)
>>> s.index.get_level_values(0)
Int64Index([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype='int64')
>>> s.index.get_level_values(1)
Index([u'a', u'b', u'c', u'a', u'b', u'c', u'a', u'b', u'c', u'a', u'b', u'c', u'a', u'b', u'c'], dtype='object')

or even:

>>> s.reset_index()
    level_0 level_1         0
0         0       a  1.419391
1         0       b  1.142944
2         0       c  0.413431
3         1       a  0.705091
4         1       b -1.846493
5         1       c -0.756824
[etc.]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing columns after applying the stack function on a pandas dataframe

From Dev

Applying a function to pandas dataframe

From Dev

Applying function to columns of a Pandas DataFrame, conditional on data type

From Dev

Applying function to Pandas dataframe by column

From Dev

Applying function to Pandas dataframe by column

From Dev

applying a lambda function to pandas dataframe

From Dev

Applying function to a DataFrame using its columns as parameters

From Dev

Applying function to dataframe columns spark scala

From Dev

Applying aggregate function on columns of Pandas pivot table

From Dev

Applying aggregate function on columns of Pandas pivot table

From Dev

Change Series inplace in DataFrame after applying function on it

From Java

Applying a custom aggregation function to a pandas DataFrame

From Dev

Applying a function to a MultiIndex pandas.DataFrame column

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Applying function to every other column in pandas dataframe

From Dev

How to subset a Pandas dataframe applying a function by date?

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Accessing hierarchical columns in pandas after groupby

From Dev

Accessing hierarchical columns in pandas after groupby

From Dev

Stack columns in pandas dataframe to achieve record format

From Dev

Applying a function to every combination of two columns in a dataframe using R

From Dev

Applying a function to every combination of two columns in a dataframe using R

From Dev

Applying lambda function on two columns in a Dataframe to get Geolocation

From Dev

applying regex to a pandas dataframe

From Dev

pandas applying multicolumnindex to dataframe

From Dev

Applying re to Pandas Dataframe

From Dev

Ho to get/return a single dictionary by applying function on pandas dataframe

From Dev

Applying function to dataframe column

From Dev

R applying function on a dataframe

Related Related

  1. 1

    Accessing columns after applying the stack function on a pandas dataframe

  2. 2

    Applying a function to pandas dataframe

  3. 3

    Applying function to columns of a Pandas DataFrame, conditional on data type

  4. 4

    Applying function to Pandas dataframe by column

  5. 5

    Applying function to Pandas dataframe by column

  6. 6

    applying a lambda function to pandas dataframe

  7. 7

    Applying function to a DataFrame using its columns as parameters

  8. 8

    Applying function to dataframe columns spark scala

  9. 9

    Applying aggregate function on columns of Pandas pivot table

  10. 10

    Applying aggregate function on columns of Pandas pivot table

  11. 11

    Change Series inplace in DataFrame after applying function on it

  12. 12

    Applying a custom aggregation function to a pandas DataFrame

  13. 13

    Applying a function to a MultiIndex pandas.DataFrame column

  14. 14

    Applying iterative function to every group in pandas DataFrame

  15. 15

    Applying function to every other column in pandas dataframe

  16. 16

    How to subset a Pandas dataframe applying a function by date?

  17. 17

    Applying iterative function to every group in pandas DataFrame

  18. 18

    Accessing hierarchical columns in pandas after groupby

  19. 19

    Accessing hierarchical columns in pandas after groupby

  20. 20

    Stack columns in pandas dataframe to achieve record format

  21. 21

    Applying a function to every combination of two columns in a dataframe using R

  22. 22

    Applying a function to every combination of two columns in a dataframe using R

  23. 23

    Applying lambda function on two columns in a Dataframe to get Geolocation

  24. 24

    applying regex to a pandas dataframe

  25. 25

    pandas applying multicolumnindex to dataframe

  26. 26

    Applying re to Pandas Dataframe

  27. 27

    Ho to get/return a single dictionary by applying function on pandas dataframe

  28. 28

    Applying function to dataframe column

  29. 29

    R applying function on a dataframe

HotTag

Archive