반응형

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)

사용하기 편하게 함수로 만들어 줍니다.

반응형
올리브한입