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

From sublists with different tuples to dataframe in Python

I have the following list:

l = [[(2, 0.23938751), (4, 0.56652236), (5, 0.1714941)],
 [(1, 0.23335448), (4, 0.21251856), (5, 0.5361737)],
 [(2, 0.27893192), (4, 0.219862), (5, 0.272998), (8, 0.1259419)],
 [(4, 0.19061193), (5, 0.35411215), (7, 0.39690432)]]

I would like to get a dataframe with two columns like the one below where "Topic" is the first element of the tuple corresponding to the max value among the second elements of each tuple.

Topic  P
  4    0.56652236
  5    0.5361737
  2    0.27893192
  7    0.39690432

Can anyone help me do it?

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

Thanks!

>Solution :

You could use max to filter the list before feeding it to the DataFrame constructor:

df = pd.DataFrame([max(e, key=lambda x: x[1]) for e in l], columns=['Topic', 'P'])

output:

   Topic         P
0      4  0.566522
1      5  0.536174
2      2  0.278932
3      7  0.396904
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