# Coursera – Introduction to Data Science in Python #12 (２週目その７)

Coursera『Introduction to Data Science in Python – by University of Michigan』の学習メモ

## Assignment 2 – Pandas Introduction

### Part 1

``````
import pandas as pd

for col in df.columns:
if col[:2]=='01':
df.rename(columns={col:'Gold'+col[4:]}, inplace=True)
if col[:2]=='02':
df.rename(columns={col:'Silver'+col[4:]}, inplace=True)
if col[:2]=='03':
df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)
if col[:1]=='№':
df.rename(columns={col:'#'+col[1:]}, inplace=True)

names_ids = df.index.str.split('\s\(') # split the index by '('

df.index = names_ids.str # the  element is the country name (new index)
df['ID'] = names_ids.str.str[:3] # the  element is the abbreviation or ID (take first 3 characters from that)

df = df.drop('Totals')
``````

#### Question 1

Which country has won the most gold medals in summer games?

sort_valuesを使って指定したカラムの降順で並び替える。

``````
# This function should return a single string value.
# 'United States'
``````

#### Question 2

Which country had the biggest difference between their summer and winter gold medal counts?

“Gold dif”カラムを追加し、夏と冬の金メダル数の差を絶対値 “abs()” で求める。

``````
# This function should return a single string value.
df["Gold dif"] = abs(df["Gold"] - df["Gold.1"])
# 'United States'
``````

#### Question 3

Which country has the biggest difference between their summer gold medal counts and winter gold medal counts relative to their total gold medal count? Only include countries that have won at least 1 gold in both summer and winter.

``````
# This function should return a single string value.
df_three = df[(df['Gold'] > 0) & (df['Gold.1']  > 0)]
df_three["Dif ratio"] = abs((df_three["Gold"] - df_three["Gold.1"]) / (df_three["Gold"] + df_three["Gold.1"]))
# 'Bulgaria'
``````

#### Question 4

Write a function that creates a Series called “Points” which is a weighted value where each gold medal (Gold.2) counts for 3 points, silver medals (Silver.2) for 2 points, and bronze medals (Bronze.2) for 1 point. The function should return only the column (a Series object) which you created, with the country names as indices.

``````
# This function should return a Series named Points of length 146
df["Points"] = (df["Gold.2"] * 3) + (df["Silver.2"] * 2) +  (df["Bronze.2"] * 1)
countries = pd.Series(df["Points"], index=df.index.values)
return countries
``````

### Part 2

``````
``````

#### Question 5

Which state has the most counties in it? (hint: consider the sumlevel key carefully! You’ll need this for future questions too…)

ドキュメントを見ると “SUMLEV” に関する説明がある。 ここでは Countryと同等レベルの “050” のみを取り扱う。

``````
# This function should return a single string value.
c_df = census_df[census_df['SUMLEV'] == 50]
c_df = c_df.groupby("STNAME").count()
return  c_df.sort_values(by='COUNTY', ascending=False).iloc.name
# 'Texas'
``````

#### Question 6

Only looking at the three most populous counties for each state, what are the three most populous states (in order of highest population to lowest population)? Use CENSUS2010POP.

``````
# This function should return a list of string values.
c_df = census_df[census_df['SUMLEV'] == 50]
df = pd.DataFrame([], columns=["STNAME", "TOTALPOP"])
index_array = c_df["STNAME"].unique()
c_df = c_df.sort_values(by='CENSUS2010POP', ascending=False)
for i in index_array:
totalpop = c_df[c_df["STNAME"] == i].iloc[:3]["CENSUS2010POP"].sum()
i_df = pd.DataFrame([[i, totalpop]], columns=["STNAME", "TOTALPOP"])
df = df.append(i_df)
``````

#### Question 7

Which county has had the largest absolute change in population within the period 2010-2015? (Hint: population values are stored in columns POPESTIMATE2010 through POPESTIMATE2015, you need to consider all six columns.)

``````
# This function should return a single string value.
c_df = census_df[census_df['SUMLEV'] == 50]
columns_to_keep = ['CTYNAME',
'POPESTIMATE2010',
'POPESTIMATE2011',
'POPESTIMATE2012',
'POPESTIMATE2013',
'POPESTIMATE2014',
'POPESTIMATE2015']
df = c_df[columns_to_keep]
columns_to_calculate = [
'POPESTIMATE2010',
'POPESTIMATE2011',
'POPESTIMATE2012',
'POPESTIMATE2013',
'POPESTIMATE2014',
'POPESTIMATE2015']
df["CHANGE"] = df[max(df[columns_to_calculate])] - df[min(df[columns_to_calculate])]
# 'Harris County'
``````

#### Question 8

In this datafile, the United States is broken up into four regions using the “REGION” column. Create a query that finds the counties that belong to regions 1 or 2, whose name starts with ‘Washington’, and whose POPESTIMATE2015 was greater than their POPESTIMATE 2014.

``````
# This function should return a 5x2 DataFrame with the columns = ['STNAME', 'CTYNAME'] and the same index ID as the census_df (sorted ascending by index).