Pandas记录

大的国家

题目

World 表:
±------------±--------+
| Column Name | Type |
±------------±--------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
±------------±--------+
name 是该表的主键(具有唯一值的列)。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。

如果一个国家满足下述两个条件之一,则认为该国是 大国 :

面积至少为 300 万平方公里(即,3000000 km2),或者
人口至少为 2500 万(即 25000000)

编写解决方案找出 大国 的国家名称、人口和面积。
按 任意顺序 返回结果表。
返回结果格式如下例所示。
示例:
输入:
World 表:
±------------±----------±--------±-----------±-------------+
| name | continent | area | population | gdp |
±------------±----------±--------±-----------±-------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
| Albania | Europe | 28748 | 2831741 | 12960000000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
| Andorra | Europe | 468 | 78115 | 3712000000 |
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
±------------±----------±--------±-----------±-------------+
输出:
±------------±-----------±--------+
| name | population | area |
±------------±-----------±--------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
±------------±-----------±--------+

官解

解决方案
方法:行过滤

  • 思路
    name continent area population gdp
    Afghanistan Asia 652230 25500100 20343000000
    Albania Europe 28748 2831741 12960000000
    Algeria Africa 2381741 37100000 188681000000
    Andorra Europe 468 78115 3712000000
    Angola Africa 1246700 20609294 100990000000

要确定一个国家是否被认为是“大国”,有两个条件需要验证,如描述中所述:

该国家的面积至少为 300 万平方千米,表示为 area >= 3,000,000。
该国家的人口至少为 2500 万,表示为 population >= 25,000,000。
  • 实现

首先,我们应用行过滤来识别满足条件的国家。

MySQL

SELECT 
    * 
FROM 
    world 
WHERE 
    area >= 3000000 
    OR population >= 25000000

Pandas

df = World[(World['area'] >= 3000000) | (World['population'] >= 25000000)]

此步骤会过滤掉不满足条件的国家行,保留剩下的表格如下。
name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Algeria Africa 2381741 37100000 188681000000

注意表格有五列,我们需要根据问题的要求返回三列,相对顺序为:name、population 和 area。

MySQL

SELECT 
    name, population, area 
FROM 
    world 
WHERE 
    area >= 3000000 
    OR population >= 25000000

Pandas

df = df[['name', 'population', 'area']]

name population area
Afghanistan 25500100 652230
Algeria 37100000 2381741

综上所述,完整的答案如下。

MySQL

SELECT
    name, population, area
FROM
    world
WHERE
    area >= 3000000 OR population >= 25000000
;

Pandas

import pandas as pd

def big_countries(World: pd.DataFrame) -> pd.DataFrame:
    df = World[(World['area'] >= 3000000) | (World['population'] >= 25000000)]
    return df[['name', 'population', 'area']]

作者:力扣官方题解
链接:https://leetcode.cn/problems/big-countries/solutions/2366084/big-countries-by-leetcode-solution-8k13/

可回收且低脂的产品

题目

表:Products
±------------±--------+
| Column Name | Type |
±------------±--------+
| product_id | int |
| low_fats | enum |
| recyclable | enum |
±------------±--------+
product_id 是该表的主键(具有唯一值的列)。
low_fats 是枚举类型,取值为以下两种 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品是低脂产品,‘N’ 表示不是低脂产品。
recyclable 是枚举类型,取值为以下两种 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品可回收,而 ‘N’ 表示不可回收。

编写解决方案找出既是低脂又是可回收的产品编号。
返回结果 无顺序要求 。
返回结果格式如下例所示:

示例 1:
输入:
Products 表:
±------------±---------±-----------+
| product_id | low_fats | recyclable |
±------------±---------±-----------+
| 0 | Y | N |
| 1 | Y | Y |
| 2 | N | Y |
| 3 | Y | Y |
| 4 | N | N |
±------------±---------±-----------+
输出:
±------------+
| product_id |
±------------+
| 1 |
| 3 |
±------------+
解释:
只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。

官解

方法:根据条件选择行

  • Pandas

我们有以下原始 DataFrame products:
product_id low_fats recyclable
0 Y N
1 Y Y
2 N Y
3 Y Y
4 N N

在 Pandas 中,布尔索引允许我们通过使用布尔数组或条件来过滤 DataFrame。这意味着我们可以使用布尔值的 Series 或创建对于 DataFrame 中每一行都评估为 True 或 False 的条件。通过将这些布尔值或条件应用到 DataFrame 的索引,我们可以选择性地提取满足条件的行。

在这种情况下,我们应该选择 low_fats 列的值为"Y"(表示产品为低脂肪产品)且 recyclable 列的值为"Y"(表示产品可回收)的行,可以表示为:
df = products[(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')]

此过滤操作创建了一个新的 DataFrame df,其中包含满足上述条件的产品。请注意,product_id 等于 0、2 和 4 的行被过滤掉了。
product_id low_fats recyclable
1 Y Y
3 Y Y

接下来,我们需要从 df 中仅选择 product_id 列,使用双方括号。
df = df[['product_id']]

得到的 DataFrame 如下所示:
product_id
1
3

完整的代码如下:

import pandas as pd

def find_products(products: pd.DataFrame) -> pd.DataFrame:
    df = products[(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')]

    df = df[['product_id']]
    
    return df
  • MySQL

关键字 SELECT 用于指定我们想要从表 Products 中检索的列。在这种情况下,我们想要检索 product_id 列。
关键字 WHERE 用于根据特定条件过滤表 Products 中的行,条件是 low_fats 列的值为"Y"(表示低脂肪产品)且 recyclable 列的值为"Y"(表示可回收产品)。我们使用逻辑运算符 AND 将两个条件组合起来,确保最终结果只包含既是低脂肪产品又是可回收产品的产品ID。

SELECT
    product_id
FROM
    Products
WHERE
    low_fats = 'Y' AND recyclable = 'Y'

作者:力扣官方题解
链接:https://leetcode.cn/problems/recyclable-and-low-fat-products/solutions/2366091/recyclable-and-low-fat-products-by-leetc-hx0p/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值