Home > database >  Is there a pandas function to get list of (index, column) with particular entries in a DataFrame?
Is there a pandas function to get list of (index, column) with particular entries in a DataFrame?

Time:02-21

Suppose, I have following dataframe:

X_train

enter image description here

Now, I would like to get list of tuples of (index, column_name) that has True values. I am not able to find any special function or method to get these except using for loop over the whole of DataFrame. Please can you help me with this?

Expected answer here would be: [('YearBuilt', 'OverallQual'), ('YearRemodAdd', 'OverallQual'), ('TotalBsmtSF', 'OverallQual')]

CodePudding user response:

Use DataFrame.stack with filter MultiIndex and convert to list of tuples:

s = df.stack()
L = s.index[s].tolist()
  • Related