python - Pandas how to copy column from one DataFrame to another with specific
问题描述
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)?
解决方案
您可以使用.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
推荐阅读
- python - 加速在 Python 中输出具有重复的产品的函数
- mysql - mysql2 捆绑安装失败
- elixir - 检查用户是否在 Phoenix 模板中进行了身份验证
- asp.net-core - 在 aspnetcore v2.1.1 上找不到 services.AddMvc()
- javascript - Typescript Async 方法是否返回承诺?
- nginx - 将实际目标路径从 nginx 模块发送到后端
- java - Java MethodHandle api 似乎产生了不正确的类型
- mysql - 尽管有条件,mysql COUNT(CASE) 返回相同的结果
- django - Django:如何更改字段的值或更新模型/数据库中的字段。?
- javascript - 使用 Styled-Components,如何在 Styled 组件之外设置一组颜色?