In [52]:
import requests
import pandas as pd
import datetime
In [91]:
# Imagine you have a master dataframe where you have been tracking observations over time.
# The code below simulates such a dataframe.
df_master = pd.DataFrame([[35.895,-4.233,7.0],[40.110,-4.855,6.0]],
index=['Dec 31 2017', 'Jan 01 2017'],
columns = ['latitude', 'longitude', 'astronauts_in_space'])
In [92]:
df_master
Out[92]:
In [93]:
# Make an HTTP get request to the API endpoint.
response = requests.get("http://api.open-notify.org/iss-now.json")
In [94]:
# A status code of 200 means the API call was processed successfully.
response.status_code
Out[94]:
In [95]:
# Read the data into a Pandas DataFrame.
df = pd.read_json(response.content)
In [96]:
# Create a date object with today's date, and format the date.
today = datetime.date.today().strftime('%b %d %Y')
In [97]:
# Rename the iss_position column as today's date.
df.rename(index=str, columns = {'iss_position':today}, inplace=True)
In [98]:
# Drop the columns we are not using.
df.drop(['message', 'timestamp'], axis=1, inplace=True)
In [99]:
# Print the current table to review progress.
df
Out[99]:
In [100]:
# Access the API endpoint specifying the number of astronauts currently aboard the ISS.
response = requests.get("http://api.open-notify.org/astros.json")
In [101]:
# Convert JSON object to dictionary and access the 'number' key.
astronauts = response.json()['number']
In [102]:
# Add another row to the dataframe with the current number of astronauts.
df.ix['astronauts_in_space'] = astronauts
In [103]:
# Transpose the dataframe so that each column can represent a separate data type.
df = df.T
df
Out[103]:
In [104]:
# Add the new observation to the collection and change the astronaut data type to integer.
df_master = df_master.append(df)
df_master['astronauts_in_space'] = [int(row) for row in df_master['astronauts_in_space']]
df_master
Out[104]:
In [ ]: