0

I created DataFrame1 using Pandas.

books_input = {"author_id" : [1, 1, 1, 2, 2, 3, 3],
               "book_title" : ['Отцы и дети', 'Рудин', 'Дворянское гнездо', 'Толстый и тонкий', 'Дама с собачкой', 'Гроза', 'Таланты и поклонники'],
               "price" : [450, 300, 350, 500, 450, 370, 290]}

DataFrame1 = pd.DataFrame(books_input)

I need to create new DataFrame called top5 and I need to take 5 highest values from "price" column from DataFrame1(need to show the most expensive books).

How I can do it? Using merging? If yes, then how I can show merge this column with specific restrictions(only 5 values)?

  • no pictures of data please, try. df.to_clipboard() and paste the data into your question instead. – Andreas Apr 26 '21 at 10:19

2 Answers2

1

You can use .nlargest()

import pandas as pd
top5 = pd.DataFrame([10,220,33,1,2,3], ["a","b","c",'x','x','x'], columns=['price'])
top5['price'].nlargest(n=5, keep='first')

Out[12]: 
b    220
c     33
a     10
x      3
x      2
Name: value, dtype: int64
Andreas
  • 8,694
  • 3
  • 14
  • 38
1

Ok so if your question is to take the all the rows with the 5 highest prices than you can do that like this:

#replace 'df' with whatever your dataframe is below
sorted_df = df.sort_values(by=['price']) 
top5 = sorted_df.iloc[:5]

Or if your question is to get the top 5 prices you can just do this:

top5 = list(reversed(sorted(df.price.values)))[:5]
Dharman
  • 30,962
  • 25
  • 85
  • 135
Lburris12
  • 13
  • 2