반응형
Table: Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| low_fats | enum |
| recyclable | enum |
+-------------+---------+
product_id is the primary key (column with unique values) for this table.
low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.
Write a solution to find the ids of products that are both low fat and recyclable.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Products table:
+-------------+----------+------------+
| product_id | low_fats | recyclable |
+-------------+----------+------------+
| 0 | Y | N |
| 1 | Y | Y |
| 2 | N | Y |
| 3 | Y | Y |
| 4 | N | N |
+-------------+----------+------------+
Output:
+-------------+
| product_id |
+-------------+
| 1 |
| 3 |
+-------------+
Explanation: Only products 1 and 3 are both low fat and recyclable.
import pandas as pd
def find_products(products: pd.DataFrame) -> pd.DataFrame:
#low_fats = products['low_fats'] == 'Y'
#recyclable = products['recyclable'] == 'Y'
#low_f_n_rcy = products[low_fats & recyclable]
#return low_f_n_rcy[['product_id']]
return products[
(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')
][['product_id']]
- (products['low_fats'] == 'Y'): 저지방 제품 필터링
- (products['recyclable'] == 'Y'): 재활용 가능 제품 필터링
- & 연산자를 통해 두 조건을 모두 만족하는 행만 추출
- 마지막에 [['product_id']]로 원하는 열만 선택
반응형
'BI & Visualization > Python' 카테고리의 다른 글
Pandas | LeetCode 1683. 15자를 초과한 트윗 찾기 (1) | 2025.05.26 |
---|---|
Pandas | LeetCode 1148. 자신이 쓴 글을 본 작가 찾기 (0) | 2025.05.25 |
Pandas | LeetCode 183. 주문하지 않은 고객 찾기 (2) | 2025.05.24 |
Pandas | LeetCode 595. World 테이블에서 큰 나라 찾기 (2) | 2025.05.22 |