chintan s
chintan s

Reputation: 6488

Pandas: get first 10 elements of a series

I have a data frame with a column tfidf_sorted as follows:

   tfidf_sorted

0  [(morrell, 45.9736796), (football, 25.58352014...
1  [(melatonin, 48.0010051405), (lewy, 27.5842077...
2  [(blues, 36.5746634797), (harpdog, 20.58669641...
3  [(lem, 35.1570832476), (rottensteiner, 30.8800...
4  [(genka, 51.4667410433), (legendaarne, 30.8800...

The type(df.tfidf_sorted) returns pandas.core.series.Series.

This column was created as follows:

df['tfidf_sorted'] = df['tfidf'].apply(lambda y: sorted(y.items(), key=lambda x: x[1], reverse=True))

where tfidf is a dictionary.

How do I get the first 10 key-value pairs from tfidf_sorted?

Upvotes: 8

Views: 36969

Answers (1)

jezrael
jezrael

Reputation: 862911

IIUC you can use:

from itertools import chain 

#flat nested lists
a = list(chain.from_iterable(df['tfidf_sorted']))
#sorting
a.sort(key=lambda x: x[1], reverse=True)
#get 10 top
print (a[:10])

Or if need top 10 per row add [:10]:

df['tfidf_sorted'] = df['tfidf'].apply(lambda y: (sorted(y.items(), key=lambda x: x[1], reverse=True))[:10])

Upvotes: 4

Related Questions