Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Suppose, I have following dataframe:

X_train

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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’)]

>Solution :

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

s = df.stack()
L = s.index[s].tolist()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading