Get the row and column labels for selected values in a Pandas dataframe

Doctor J

I'd like to get the row and column labels for values matching some condition in a dataframe. Just to keep it interesting, I need it to work with a hierarchical (multi-)index. For example:

df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=pd.MultiIndex.from_product((('a', 'b'), ('x', 'y'))))

    a       b    
    x   y   x   y
0   0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15

Now let's say I want the row and column labels of the elements where

df % 6 == 0

       a             b       
       x      y      x      y
0   True  False  False  False
1  False  False   True  False
2  False  False  False  False
3   True  False  False  False

I would like to get

[(0, ('a', 'x')), (1, ('b', 'x')), (3, ('a', 'x'))]

Please note I would like a general solution, that does not rely on the index being monotonic, or the particular selection in my example. This questions has been asked many times, but the answers do not generalize:

Is this really so hard in Pandas?

unutbu

Use np.where to obtain the ordinal indices of the True values:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.arange(16).reshape(4, 4), 
                  columns=pd.MultiIndex.from_product((('a', 'b'), ('x', 'y'))))

mask = (df % 6 == 0)
i, j = np.where(mask)
print(list(zip(df.index[i], df.columns[j])))

yields

[(0, ('a', 'x')), (1, ('b', 'x')), (3, ('a', 'x'))]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get the row and column labels for selected values in a Pandas dataframe

From Dev

Row Values to Column Array in Pandas DataFrame

From Dev

Mapping row-wise sorted dataframe to original column labels (Pandas)

From Dev

how to replace values of selected row of a column in panda's dataframe?

From Dev

Get list from pandas dataframe column or row?

From Dev

Repeat Pandas dataframe row labels

From Dev

Use column values in Pandas DataFrame to populate string row by row

From Dev

Pandas DataFrame get column combined max values

From Dev

Pandas DataFrame get column combined max values

From Dev

How to get selected row's all column values of dataGridView in TextBoxes

From Dev

Pandas Convert Dataframe Values to Labels

From Dev

Column to row in pandas dataframe

From Dev

Efficiently transforming pandas dataframe column names into row values

From Java

Pandas Dataframe row selection combined condition index- and column values

From Dev

Find unique values in a Pandas dataframe, irrespective of row or column location

From Dev

Assigning multiple column values in a single row of pandas DataFrame, in one line

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

Pandas Set multiple column and row values to nan based on another dataframe

From Dev

Pandas Dataframe row selection combined condition index- and column values

From Dev

Display row with False values in validated pandas dataframe column

From Dev

Efficiently transforming pandas dataframe column names into row values

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

Pandas dataframe: Is there a difference in performance in replacing values by column and row?

From Dev

Pandas dataframe - Rearranging row index values into column headers

From Dev

Turn values in a DataFrame column into column labels

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

From Dev

How to get column name for second largest row value in pandas DataFrame

From Dev

Get Column and Row Index for Highest Value in Dataframe Pandas

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

Related Related

  1. 1

    Get the row and column labels for selected values in a Pandas dataframe

  2. 2

    Row Values to Column Array in Pandas DataFrame

  3. 3

    Mapping row-wise sorted dataframe to original column labels (Pandas)

  4. 4

    how to replace values of selected row of a column in panda's dataframe?

  5. 5

    Get list from pandas dataframe column or row?

  6. 6

    Repeat Pandas dataframe row labels

  7. 7

    Use column values in Pandas DataFrame to populate string row by row

  8. 8

    Pandas DataFrame get column combined max values

  9. 9

    Pandas DataFrame get column combined max values

  10. 10

    How to get selected row's all column values of dataGridView in TextBoxes

  11. 11

    Pandas Convert Dataframe Values to Labels

  12. 12

    Column to row in pandas dataframe

  13. 13

    Efficiently transforming pandas dataframe column names into row values

  14. 14

    Pandas Dataframe row selection combined condition index- and column values

  15. 15

    Find unique values in a Pandas dataframe, irrespective of row or column location

  16. 16

    Assigning multiple column values in a single row of pandas DataFrame, in one line

  17. 17

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  18. 18

    Pandas Set multiple column and row values to nan based on another dataframe

  19. 19

    Pandas Dataframe row selection combined condition index- and column values

  20. 20

    Display row with False values in validated pandas dataframe column

  21. 21

    Efficiently transforming pandas dataframe column names into row values

  22. 22

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  23. 23

    Pandas dataframe: Is there a difference in performance in replacing values by column and row?

  24. 24

    Pandas dataframe - Rearranging row index values into column headers

  25. 25

    Turn values in a DataFrame column into column labels

  26. 26

    Get column and row index pairs of Pandas DataFrame matching some criteria

  27. 27

    How to get column name for second largest row value in pandas DataFrame

  28. 28

    Get Column and Row Index for Highest Value in Dataframe Pandas

  29. 29

    Get column and row index pairs of Pandas DataFrame matching some criteria

HotTag

Archive