Home > Net >  Choose Sentences with x High Score and Save to the Same Row (Dataframe)
Choose Sentences with x High Score and Save to the Same Row (Dataframe)

Time:02-15

I have Dataframe ranked_sentences like this

       0              1
0   0.093305    Hello this is a
1   0.088413    Hello this is b
2   0.091036    Hello this is c
3   0.095541    Hello this is d
4   0.092201    Hello this is e
5   0.088338    Hello this is f
6   0.089100    Hello this is g
7   0.089980    Hello this is h
8   0.088446    Hello this is i
9   0.092737    Hello this is j
10  0.090902    Hello this is k

i want to choose sentences with sn (sn=4) high score and save in list. moreover, a sentence order according to index. I want the output like this:

                                    x
0   Hello this is a. Hello this is d. Hello this is e. Hello this is j.

anybody can help me to get my goal. I try with this code, but i confused for get sentences with high score. thank you

d = []
sn = 4
for i in range(sn):
    summary = ranked_sentences[1][i] 
    d.append(summary)
    print(summary)

CodePudding user response:

This below code will

  1. sort the dataframe on column 0 in descending order.
  2. Fetch top 3 rows.
  3. Convert the 3 values of column 1 to a list
  4. Assign the list to list d.
d = ranked_sentences.sort_values(0, ascending = False).head(3)[1].to_list()
  • Related