Suppose, I have following dataframe:
X_train
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()