본문 바로가기

Algorithm/Python

[stratascratch] number of bathrooms and bedrooms(Python)

문제

Number Of Bathrooms And Bedrooms

Find the average number of bathrooms and bedrooms for each city’s property types. Output the result along with the city name and the property type.

각각의 city와 property_type별로 bedrooms와 bathrooms의 평균 구하기

코드

airbnb_search_details.groupby(['city','property_type'])[['bedrooms','bathrooms']].mean()

인덱스에 있는 값을 컬럼으로 빼주기 위해서 reset_index() 사용

# Import your libraries
import pandas as pd

# Start writing code
airbnb_search_details.head()
add = airbnb_search_details.groupby(['city','property_type'])[['bedrooms','bathrooms']].mean()
add.reset_index()

출처

반응형