파이썬 기초 익히기3
Q44. 데이터를 로드하고 상위 5개 컬럼을 출력하라
import pandas as pd
DriveUrl = 'https://drive.google.com/~'
df= pd.read_csv(DriveUrl)
Ans =df.head(5)
Ans
Q45. 데이터의 각 host_name의 빈도수를 구하고 host_name으로 정렬하여 상위 5개를 출력하라
#1
Ans = df.groupby('host_name').size().sort_index()
#2
Ans = df.host_name.value_counts().sort_index()
Q46. 데이터의 각 host_name의 빈도수를 구하고 빈도수로 정렬하여 상위 5개를 출력하라
#1
df.host_name.value_counts().to.frame().head()
Ans = df.groupby('host_name').size().₩
to.frame().rename(columns={0:'counts'}.₩
sort_values('counts',ascending=False)
Ans.head()
Q47. neighbourhood_group의 값에 따른 neighbourhood컬럼 값의 갯수를 구하여라
Ans = df.groupby(['neighbourhood_group','neighbourhood'],as_index=False).size()
Ans.head()
Q48. neighbourhood_group의 값에 따른 neighbourhood컬럼 값 중 neighbourhood_group그룹의 최댓값들을 출력하라
Ans = df.groupby(['neighbourhood_group','neighbourhood'],as_index=False).size()₩
.groupby(['neighbourhood_group'],as_index=False).max()
Ans
Q49. neighbourhood_group의 값에 따른 price컬럼 값 중 neighbourhood_group그룹의 최댓값들을 출력하라
Ans = df.groupby('neighbourhood_group')['price'].agg(['mean','var','max','min'])
Ans
Q50. neighbourhood_group 값에 따른 reviews_per_month 평균, 분산, 최대, 최소 값을 구하여라
Ans = df.groupby('neighbourhood_group')['reviews_per_month'].agg(['mean','var','max','min'])
Ans
Q51.neighbourhood 값과 neighbourhood_group 값에 따른 price 의 평균을 구하라
Ans = df.groupby(['neighbourhood_group',' neighbourhood'],as_index=False).price.mean()
Ans
Q52. neighbourhood 값과 neighbourhood_group 값에 따른 price 의 평균을 계층적 indexing 없이 구하라
Ans = df.groupby(['neighbourhood_group',' neighbourhood'],as_index=False).price.mean().unstack()
Ans
Q53. neighbourhood 값과 neighbourhood_group 값에 따른 price 의 평균을 계층적 indexing 없이 구하고 nan 값은 -999값으로 채워라
Ans = df.groupby(['neighbourhood_group',' neighbourhood'],as_index=False).price.mean()₩
.unstack().fillna(-999)
Ans
Q54. 데이터중 neighbourhood_group 값이 Queens값을 가지는 데이터들 중 neighbourhood 그룹별로 price값의 평균, 분산, 최대, 최소값을 구하라
Ans = df.[df.neighbourhood_group == 'Queens'].groupby(['neighbourhood']).₩
.price.agg(['mean','var','max','min'])
#Ans.sort_values('mean',ascending=False)
Ans
Q55. 데이터중 neighbourhood_group 값에 따른 room_type 컬럼의 숫자를 구하고 neighbourhood_group 값을 기준으로 각 값의 비율을 구하여라
Ans = df[['neighbourhood_group','room_type']]. ## grouping에 필요한 컬럼만 뽑아서 groupby
groupby(['neighbourhood_group','room_type']).size().unstack()
Ans.loc[:,:] = (Ans.values/Ans.sum(axis=1).values.reshape(-1,1)
Ans
'제로베이스 > Python' 카테고리의 다른 글
| Python 6. Pivot (0) | 2024.06.29 |
|---|---|
| Python 5. Time_Series (0) | 2024.06.29 |
| Python 4.Apply, Map (0) | 2024.06.28 |
| Python 2. Filtering & Sorting (0) | 2024.06.27 |
| Python 1. Getting & Knowing Data (0) | 2024.06.27 |