Home > Back-end >  From sublists with different tuples to dataframe in Python
From sublists with different tuples to dataframe in Python

Time:02-16

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?

Thanks!

CodePudding user response:

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
  • Related