본문 바로가기

Algorithm/Python

[stratascratch] Number Of Units Per Nationality(Python)

문제

Find the number of apartments per nationality that are owned by people under 30 years old. Output the nationality along with the number of apartments. Sort records by the apartments count in descending order.

  • 30세 미만 인구가 소유한 국적별 아파트 수 조회하기
  • 아파트 수를 기준으로 내림차순 정렬

코드

# Import your libraries
import pandas as pd

# Start writing code
airbnb_hosts.head()
df = pd.merge(airbnb_hosts, airbnb_units, how="inner", on="host_id")
# 30세 미만 & 아파트 소유 조건
result = df[(df.age < 30) & (df.unit_type == 'Apartment')]
# nationality 별로 count하기
result = result.groupby(['nationality'])['unit_type'].count().reset_index()
# unit_type을 기준으로 내림차순 정렬
result = result.sort_values(by="unit_type", ascending=False)
result.rename(columns = {'unit_type':'apartment_count'}, inplace=True)
result

출처

반응형