print('Hello World')Hello World
Hello World!
Please come to office hours to get help!
Be patient and kind with yourself and others. Sometimes getting started can be overwhelming, DON’T GIVE UP, you can do this and I can help!
I can be REALLY FLEXIBLE with deadlines in the first few weeks of class, so if you run into technology issues please don’t panic.
Day2 folder in your repo.The first program you write in almost any language is called Hello World.
You can type this code into the notebook or copy and paste it and then run the cell (press play or use SHIFT-ENTER).
print('Hello World')Hello World
Q. How would you make python print your name? Try making changes!
Now that you have officially programmed something in Python, let’s start doing Data Science!
Python is organized into packages called modules. When you want to use certain programs (functions) you need to install and import the module.
Good news - you already installed everything you need for the whole semester when you ran pip install -r requirements.txt during Day 1 setup. Nothing to install here! But if you get errors running the next cell, it is probably that you forgot to install the modules.
At the top of every JupyterLab Notebook, you will see a bunch of package imports. You are basically telling Python what extra functions you will need. You can just run this cell (press play or use SHIFT-ENTER).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.io as pio
pio.renderers.default='notebook_connected'Below is code to explore our first data set. This exploration is from https://datasciencebox.org/ and the original author of this content (written in R) is Mine Çetinkaya-Rundel. I have updated it for our class and translated the code to python.
Introduction
How do various countries vote in the United Nations General Assembly, how have their voting patterns evolved throughout time, and how similarly or differently do they view certain issues? Answering these questions (at a high level) is the focus of this analysis.
Data
The data we’re using originally come from the unvotes R package. This package provides the voting history of countries in the United Nations General Assembly and the original data can be found HERE.
The data in the .csv (comma separated value) file has been joined in R to help with the analysis
The code below grabs the data from the internet and saves at a Pandas DataFrame named \(DF\).
# Note this takes about a minute to run
file_location = 'https://joannabieri.com/introdatascience/data/unvotes.csv'
DF = pd.read_csv(file_location)
years = [int(d.split('-')[0]) for d in DF['date']]
DF['year'] = years
DF = DF.drop('Unnamed: 0',axis=1)::: {#cell-Show data .cell execution=‘{“iopub.execute_input”:“2026-08-26T20:34:04.184544Z”,“iopub.status.busy”:“2026-08-26T20:34:04.184369Z”,“iopub.status.idle”:“2026-08-26T20:34:04.199264Z”,“shell.execute_reply”:“2026-08-26T20:34:04.198746Z”}’ execution_count=4}
display(DF)| rcid | country | country_code | vote | session | importantvote | date | unres | amend | para | short | descr | short_name | issue | year | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6 | United States | US | no | 1 | 0.0 | 1946-01-04 | R/1/107 | 0.0 | 0.0 | DECLARATION OF HUMAN RIGHTS | TO ADOPT A CUBAN PROPOSAL (A/3-C) THAT AN ITEM... | hr | Human rights | 1946 |
| 1 | 6 | Canada | CA | no | 1 | 0.0 | 1946-01-04 | R/1/107 | 0.0 | 0.0 | DECLARATION OF HUMAN RIGHTS | TO ADOPT A CUBAN PROPOSAL (A/3-C) THAT AN ITEM... | hr | Human rights | 1946 |
| 2 | 6 | Cuba | CU | yes | 1 | 0.0 | 1946-01-04 | R/1/107 | 0.0 | 0.0 | DECLARATION OF HUMAN RIGHTS | TO ADOPT A CUBAN PROPOSAL (A/3-C) THAT AN ITEM... | hr | Human rights | 1946 |
| 3 | 6 | Dominican Republic | DO | abstain | 1 | 0.0 | 1946-01-04 | R/1/107 | 0.0 | 0.0 | DECLARATION OF HUMAN RIGHTS | TO ADOPT A CUBAN PROPOSAL (A/3-C) THAT AN ITEM... | hr | Human rights | 1946 |
| 4 | 6 | Mexico | MX | yes | 1 | 0.0 | 1946-01-04 | R/1/107 | 0.0 | 0.0 | DECLARATION OF HUMAN RIGHTS | TO ADOPT A CUBAN PROPOSAL (A/3-C) THAT AN ITEM... | hr | Human rights | 1946 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 857873 | 9101 | Venezuela | VE | yes | 74 | NaN | 2019-12-03 | A/RES/74/12 | NaN | NaN | Division for Palestinian Rights of the Secreta... | Division for Palestinian Rights of the Secreta... | me | Palestinian conflict | 2019 |
| 857874 | 9101 | Vietnam | VN | yes | 74 | NaN | 2019-12-03 | A/RES/74/12 | NaN | NaN | Division for Palestinian Rights of the Secreta... | Division for Palestinian Rights of the Secreta... | me | Palestinian conflict | 2019 |
| 857875 | 9101 | Yemen | YE | yes | 74 | NaN | 2019-12-03 | A/RES/74/12 | NaN | NaN | Division for Palestinian Rights of the Secreta... | Division for Palestinian Rights of the Secreta... | me | Palestinian conflict | 2019 |
| 857876 | 9101 | Zambia | ZM | yes | 74 | NaN | 2019-12-03 | A/RES/74/12 | NaN | NaN | Division for Palestinian Rights of the Secreta... | Division for Palestinian Rights of the Secreta... | me | Palestinian conflict | 2019 |
| 857877 | 9101 | Zimbabwe | ZW | yes | 74 | NaN | 2019-12-03 | A/RES/74/12 | NaN | NaN | Division for Palestinian Rights of the Secreta... | Division for Palestinian Rights of the Secreta... | me | Palestinian conflict | 2019 |
857878 rows × 15 columns
:::
In the table above, see if you can look up information from a UN Country. For example, try running DF[DF["country"] == "Canada"] in a new cell to see just that country’s rows. See if you can answer the following questions:
Q. How many columns are there?
Q. Can you guess what each column data represents? Try to figure it out, but it’s okay if right now your answer is “No Idea!”
Harder Q. How many different countries are there in the data set?
Harder Q. How many rows are there in the data set?
*Note - things listed as Harder Q. are questions that would be hard to answer without more help from python.
::: {#cell-How many countries? Part 1 .cell execution=‘{“iopub.execute_input”:“2026-08-26T20:34:04.200958Z”,“iopub.status.busy”:“2026-08-26T20:34:04.200782Z”,“iopub.status.idle”:“2026-08-26T20:34:04.236970Z”,“shell.execute_reply”:“2026-08-26T20:34:04.236435Z”}’ execution_count=5}
# Python can list all the different countries:
country_list = list(DF['country'].unique())
# Show the data in a nice way
display(pd.DataFrame(country_list,columns=['country']))| country | |
|---|---|
| 0 | United States |
| 1 | Canada |
| 2 | Cuba |
| 3 | Dominican Republic |
| 4 | Mexico |
| ... | ... |
| 195 | Kiribati |
| 196 | Switzerland |
| 197 | Timor-Leste |
| 198 | Montenegro |
| 199 | South Sudan |
200 rows × 1 columns
:::
# Python can count up the number of countries.
# Find the length of the list
print(len(country_list))200
# Optional - Make a nice print statement
print(f'There are {len(country_list)} in our data set!')There are 200 in our data set!
You Try
See if you can figure out how the change the code below to look at the column labeled “issue”. The goal is to see a list of all the issues. Change the ??? part of the code.
issues_list = list(DF[???].unique())
issues_listLet’s create a data visualisation that displays how the voting record of the US changed over time on a variety of issues, and compares it to two other countries: UK and Turkey.
We can easily change which countries are being plotted by changing which countries the code above filters for. Note that the country name should be spelled and capitalized exactly the same way as it appears in the data.
The code below does the following:
countries = ['Turkey', 'United States', 'United Kingdom']
issues = list(DF['issue'].unique())
c_groups = DF.groupby(['country','issue'])
print(issues)['Human rights', 'Economic development', 'Colonialism', 'Palestinian conflict', 'Arms control and disarmament', 'Nuclear weapons and nuclear material']
Now make a pretty picture
There is some more complicated code here to create a beautiful picture, but for now all you need to do is run the code. As the semester goes on we will learn how to make our own beautiful pictures!
def make_plot(countries,issue):
'''
A Python function that takes in the list of countries and issues and makes
a scatter plot of each issue with a trendline for each country.
'''
x_data = []
y_data = []
c_data = []
for cntry in countries:
my_group = c_groups.get_group((cntry,issue))
for y in my_group['year'].unique():
x_data.append(y)
tot_yes = sum(my_group[my_group['year']==y]['vote']=='yes')
percent_yes = tot_yes/len(my_group[my_group['year']==y])*100
y_data.append(percent_yes)
c_data.append(cntry)
fig = px.scatter(x=x_data, y=y_data,color=c_data,trendline="lowess",labels={"color": "Country"})
fig.update_layout(
title={
'text': issue + '<br>',
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
fig.update_yaxes(title_text="% Yes")
fig.update_xaxes(title_text="Year")
f_name = issue
# fig.write_image(f_name+'.png') # PNGs already saved in images/; needs kaleido+Chrome to regenerate
fig.show()
for iss in issues:
make_plot(countries,iss)