Home>

In a table like the one below, I would like to get the row and column numbers (or row, column names) with element 1 in tabular form. At this time, is there a way to do it without using the for statement?

A B
a 0 0
b 0 1
c 1 0
df = pd.DataFrame ([[0,0], [0,1], [1,0]], columns = ['A','B'], index = ['a','b',' c'])
How to use # for statement (I really want to do it without using for statement)
result = []
for i in range (len (df.index)):
    for j in range (len (df.columns)):
        if df.iloc [i, j] == 1:
            result.append ((i, j))
print (result) # [(1, 1), (2, 0)]
  • Answer # 1

    It seems that pandas alone can not be done without using for, but it seems that it can be done by using numpy's nonzero function.

    import pandas as pd
    df = pd.DataFrame ([[0,0], [0,1], [1,0]], columns = ['A','B'], index = ['a','b',' c'])
    arr = (df == 1) .to_numpy ()
    result = list (zip (* arr.nonzero ()))
    print (result)
    # [(1, 1), (2, 0)]

    The nonzero function returns the index of a non-zero (not False this time) element.
    What is returned is the tuple shape (0-dimensional index array, 1-dimensional index array) (in this example ([1, 2], [1, 0])).
    So I use zip to transform it into an array of tuples (0D index, 1D index).