dylkot
dylkot

Reputation: 2475

Getting (index, column) pairs for True elements of a boolean DataFrame in Pandas

Say I have a Pandas DataFrame and I want to obtain a list of tuples of the form [(index1, column1), (index2, column2) ...] describing the locations of all elements of the DataFrame where some condition is true. For example:

x = pd.DataFrame(np.random.normal(0, 1, (4,4)), index=['a', 'b', 'c', 'd'],
                 columns=['e', 'f', 'g', 'h'])
x


     e           f           g           h
a   -1.342571   -0.274879   -0.903354   -1.458702
b   -1.521502   -1.135800   -1.147913   1.829485
c   -1.199857   0.458135    -1.993701   -0.878301
d   0.485599    0.286608    -0.436289   -0.390755

y = x > 0

Is there any way to obtain:

x.loc[y]

To return:

[(b, h), (c,f), (d, e), (d,f)]

Or some equivalent? Evidently, I can do:

postup = []
for i in x.index:
    for j in x.columns:
        if x.loc[i, j] > 0:
            postup.append((i, j))

But I imagine something better may be possible / already implemented. In matlab the function find combined with sub2ind do the job.

Upvotes: 20

Views: 6554

Answers (3)

A. Coady
A. Coady

Reputation: 57398

x[x > 0].stack().index.tolist()

Upvotes: 28

allen-smithee
allen-smithee

Reputation: 671

If you want a single tuple for each row index:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.normal(0, 1, (4,4)), index=['a', 'b', 'c', 'd'], columns=['e', 'f', 'g', 'h'])

# build column replacement
column_dict = {}
for col in [{col: {True: col}} for col in df.columns]:
    column_dict.update(col)

# replace where > 0
df = (df>0).replace(to_replace=column_dict)

# convert to tuples and drop 'False' values
[tuple(y for y in x if y != False) for x in df.to_records()]

Upvotes: 2

exp1orer
exp1orer

Reputation: 12039

My approach uses MultiIndex:

#make it a multi-indexed Series
stacked = y.stack()

#restrict to where it's True
true_stacked = stacked[stacked]

#get index as a list of tuples
result = true_stacked.index.tolist()

Upvotes: 3

Related Questions