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

I have dataframe. I need to create a dictionary with row as the key and columns which has 'True' as the values of the dictionary

     0    1      2      3      4      5      6      7      8      9  
3  False   True  False   True   True  False   True   True   True  False      
4  False   True  False   True   True   True   True   True  False  False      

I need to create something like this

dict= {3: [1,3,4,6,7,8], 4: [0,1,3,4,5,6,7]}

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

>Solution :

I hope I’ve understood your question right:

out = dict(
    df.apply(lambda x: [int(i) for i, v in zip(x.index, x) if v], axis=1)
)
print(out)

Prints:

{3: [1, 3, 4, 6, 7, 8], 4: [1, 3, 4, 5, 6, 7]}
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