条件筛选
- 1. 相关知识点
- 1.1 query条件或查询
- 1.2 query条件与查询
- 1.3 存在查询及列名修改
- 1.4 条件查询,相等
- 1.5 删除重复值
- 1.6 排序
- 2. 题目
- 2.1 大的国家
- 2.2 可回收且低脂的产品
- 2.3 从不订购的客户
- 2.4 文章浏览 I
1. 相关知识点
1.1 query条件或查询
# |或
world.query('`area`>= 3000000 | `population`>=25000000')[['name','population','area']]
1.2 query条件与查询
# &与
products.query('`low_fats`=="Y" & `recyclable`=="Y"')[['product_id']]
1.3 存在查询及列名修改
customers=customers.query(f'`id` not in {order_list}')
customers=customers.rename(columns={'name':'Customers'})
1.4 条件查询,相等
new_views=views[views['author_id']==views['viewer_id']]
1.5 删除重复值
new_views.drop_duplicates(keep='first',inplace=True)
1.6 排序
new_views.sort_values(by='author_id',inplace=True)
2. 题目
2.1 大的国家
import pandas as pddef big_countries(world: pd.DataFrame) -> pd.DataFrame:world_new=world.query('`area`>= 3000000 | `population`>=25000000')[['name','population','area']]return world_new
2.2 可回收且低脂的产品
import pandas as pddef find_products(products: pd.DataFrame) -> pd.DataFrame:return products.query('`low_fats`=="Y" & `recyclable`=="Y"')[['product_id']]
2.3 从不订购的客户
import pandas as pddef find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:order_list=orders['customerId'].to_list()customers=customers.query(f'`id` not in {order_list}')customers=customers.rename(columns={'name':'Customers'})return customers[['Customers']]
2.4 文章浏览 I
import pandas as pddef article_views(views: pd.DataFrame) -> pd.DataFrame:new_views=views[views['author_id']==views['viewer_id']][['author_id']].drop_duplicates().sort_values(by='author_id').rename(columns={'author_id':'id'})# new_views=views[views['author_id']==views['viewer_id']][['author_id']]# new_views.drop_duplicates(keep='first',inplace=True)# new_views.sort_values(by='author_id',inplace=True)# new_views.columns=['id']return new_views