반응형
Table: World
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
name is the primary key (column with unique values) for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.
A country is big if:
- it has an area of at least three million (i.e., 3000000 km2), or
- it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
World table:
+-------------+-----------+---------+------------+--------------+
| 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 |
+-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+
| name | population | area |
+-------------+------------+---------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+-------------+------------+---------+
import pandas as pd
world = pd.read_excel('pandas1.xlsx')
pandas를 불러오고, 임의로 만든 excel 파일을 읽어 줍니다.ㄷ
world['area'] >= 3000000
world['population'] >= 25000000
(world['area'] >= 3000000) | (world['population'] >= 25000000)
위의 코드를 실행하여 두 조건 중 하나라도 만족하는 나라 필터링 합니다.
Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
Algeria | Africa | 2381741 | 37100000 | 188681000000 |
world[(world['area'] >= 3000000) | (world['population'] >= 25000000)][['name', 'population', 'area']]
필요한 컬럼만 반환합니다.
def big_countries(world: pd.DataFrame) -> pd.DataFrame:
# 조건 1: 면적이 3000000 이상
large_area = world['area'] >= 3000000
# 조건 2: 인구가 25000000 이상
large_population = world['population'] >= 25000000
# 두 조건 중 하나 이상 만족하는 나라
big = world[large_area | large_population]
# name, population, area 컬럼만 선택해서 반환
return big[['name', 'population', 'area']]
# 함수 실행
result = big_countries(world)
사용하기 편하게 함수로 만들어 줍니다.
반응형
'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 1757. 연산자를 사용하여 조건 만족하는 행 추출하기 (3) | 2025.05.23 |