2 minute read

전세계 행복도 지표 데이터

import pandas as pd
df =pd.read_csv('https://raw.githubusercontent.com/Datamanim/datarepo/main/happy2/happiness.csv',encoding='utf-8')
df.head()
행복랭킹 나라명 점수 상대GDP 사회적지원 행복기대치 선택의 자유도 관대함 부패에 대한인식 년도
0 1 Finland 7.769 1.340 1.587 0.986 0.596 0.153 0.393 2019
1 2 Denmark 7.600 1.383 1.573 0.996 0.592 0.252 0.410 2019
2 3 Norway 7.554 1.488 1.582 1.028 0.603 0.271 0.341 2019
3 4 Iceland 7.494 1.380 1.624 1.026 0.591 0.354 0.118 2019
4 5 Netherlands 7.488 1.396 1.522 0.999 0.557 0.322 0.298 2019

Q44. 2018년도와 2019년도의 행복랭킹이 변화하지 않은 나라명의 수를 구하여라

datamanim 에서는 엄청 간단하게 풀이하고 있다.
2018,2019 두 개의 구분자 밖에 없으니 [‘행복랭킹’, ‘나라명’] 을 중복제거 했을떄 없어지는게 변화하지 않은 것이다..

df[df[['행복랭킹','나라명']].duplicated()].sort_values('행복랭킹')  # .shape[0]을 찍으면 15개 인것을 알 수 있다.
행복랭킹 나라명 점수 상대GDP 사회적지원 행복기대치 선택의 자유도 관대함 부패에 대한인식 년도
156 1 Finland 7.632 1.305 1.592 0.874 0.681 0.202 0.393 2018
159 4 Iceland 7.495 1.343 1.644 0.914 0.677 0.353 0.138 2018
163 8 New Zealand 7.324 1.268 1.601 0.876 0.669 0.365 0.389 2018
177 22 Malta 6.627 1.270 1.525 0.884 0.645 0.376 0.142 2018
189 34 Singapore 6.343 1.529 1.451 1.008 0.631 0.261 0.457 2018
208 53 Latvia 5.933 1.148 1.454 0.671 0.363 0.092 0.066 2018
211 56 Jamaica 5.890 0.819 1.493 0.693 0.575 0.096 0.031 2018
215 60 Kazakhstan 5.790 1.143 1.516 0.631 0.454 0.148 0.121 2018
220 65 Peru 5.663 0.934 1.249 0.674 0.530 0.092 0.034 2018
231 76 Hong Kong 5.430 1.405 1.290 1.030 0.524 0.246 0.291 2018
278 123 Mozambique 4.417 0.198 0.902 0.173 0.531 0.206 0.158 2018
294 139 Togo 3.999 0.259 0.474 0.253 0.434 0.158 0.101 2018
298 143 Madagascar 3.774 0.262 0.908 0.402 0.221 0.155 0.049 2018
308 153 Tanzania 3.303 0.455 0.991 0.381 0.481 0.270 0.097 2018
310 155 Central African Republic 3.083 0.024 0.000 0.010 0.305 0.218 0.038 2018

좀 복잡하게 푼 내 풀이를 정리해보곘다. (pivot_table)

df_new = pd.pivot_table(df, index = '행복랭킹', columns = '년도' , values = '나라명', aggfunc = lambda x : '-'.join(x))  # pivot_table 만드는 법 숙지!
df_new.head()
년도 2018 2019
행복랭킹
1 Finland Finland
2 Norway Denmark
3 Denmark Norway
4 Iceland Iceland
5 Switzerland Netherlands
df_new['chk'] = df_new[2018] == df_new[2019]
df_new[df_new.chk == True]  # .shape[0] 찍으면 15인 것을 알 수 있다.
년도 2018 2019 chk
행복랭킹
1 Finland Finland True
4 Iceland Iceland True
8 New Zealand New Zealand True
22 Malta Malta True
34 Singapore Singapore True
53 Latvia Latvia True
56 Jamaica Jamaica True
60 Kazakhstan Kazakhstan True
65 Peru Peru True
76 Hong Kong Hong Kong True
123 Mozambique Mozambique True
139 Togo Togo True
143 Madagascar Madagascar True
153 Tanzania Tanzania True
155 Central African Republic Central African Republic True

Leave a comment