Chapter 6 problem set 2 - UCD-pbio-rclub/python_problems GitHub Wiki
Ch. 6
We are going to being looking at NFL player stats for the 2018 season
- Using the information provided here create the proper url to use with
requests.get(url)
to get the overall stats for each player for the season.
Answer
url = 'http://api.fantasy.nfl.com/v1/players/stats'
resp = requests.get(url)
resp
- Process the JSON of the response and create a pandas data frame with columns for each player's name, position, team, and season points
Answer
data = resp.json()
data2 = data['players']
data3 = pd.DataFrame(data2, columns=['name', 'position','teamAbbr', 'seasonPts'])
- Find the top five quarterbacks this season based on points
Answer
data3[data3['position']=='QB'].sort_values(by=['seasonPts'], ascending=False).head()
This is an API that might be cool to to use also in the next chapter when we use regular expressions. It can search for lyrics, using the artist/band and song
The url is https://api.lyrics.ovh/v1/
First, we define the parameters of the search (band AND song)
artist = "queen" #try to use full names
song = "bohemian rhapsody" #with spaces
# This is how the url would look:
url = 'https://api.lyrics.ovh/v1/' #an API that searches lyrics
url= url + artist.lower() + "/" + song.lower()
- How would you interact with the API in python to get the lyrics?
Answer
import requests
request = requests.get(url)
request #response should be 200
response = request.json()
- What is returned?
Answer
print (response)
3. what type of object is returned by the request?
Answer
type(resp)
3.b
Could you directly update the key to contain the name of the song instead of the text "lyrics"?
```python Not really, keys are immutable ```
- How can you unpack the lyrics into a string object?
Answer
lyrics = list(response.values())[0]
print (lyrics)
There is a RESTful API to query NWS weather forecasts.
I am going to make your life somewhat easier by providing you with a URL that you can use:
https://api.weather.gov/points/38.5449%2C-121.7405/forecast
will get you the forecast for Davis.
In python get the data returned by that url and parse it to give you a data frame with the name of the day, temperature, wind speed and direction, and the detailed forecast.
Answer
import requests
import pandas as pd
url = "https://api.weather.gov/points/38.5449%2C-121.7405/forecast"
resp = requests.get(url)
data = resp.json()
weather = pd.DataFrame(data['properties']['periods'], columns=['name', 'temperature', 'windSpeed', 'windDirection', 'detailedForecast'])
Answer
# Download the excel file from the website.
xlsx = pd.ExcelFile('HW6/WDSF Athletes on 2019-01-20.xlsx')
Athletes = pd.read_excel(xlsx, 'Couples')
Athletes.head()
2. Please calculate the number of athletes from each country. Which countries are the top 3 countries have the highest number of athletes in dancesport?
Answer
Athletes_Representing = Athletes['Representing']
Athletes_Representing.value_counts()
3. Let's focus on the top1 country that you find from question 2. Make a new excel file with only the athletes from this country and please calculate the number of athletes from each age group in this country.
Answer
Top1Athletes = Athletes.Representing.value_counts().head(1).index[0]
Top1Athletes
RussiaAthletes = Athletes[Athletes.Representing == Top1Athletes]
RussiaAthletes
RussiaAthletes.to_excel('HW6/RussiaAthletes.xlsx')
RAthletes_Age = RussiaAthletes['Age group']
RAthletes_Age.value_counts()
I used the stat from Golden State Warriors (https://www.nba.com/warriors/stats).
- Get the players' stat. (This site contains two tables, so pick up one of the tables to work with).
Answer
data = pd.read_html('https://www.nba.com/warriors/stats', header=0)
table1 = data[0]
# I tried to use requests.get(), but got an error message.
- Convert the extracted data to binary format.
Answer
dat2 = dat1.to_pickle('dat2_pickle')
pd.read_pickle('dat2_pickle') #just for confirmation
From the 1st 250 inputs of the UC Davis page on thegradcafe (https://www.thegradcafe.com/survey/index.php?q=uc+davis&t=a&o=&pp=250)
- Get the DataFrame
Answer
frame = pd.read_html('https://www.thegradcafe.com/survey/index.php?q=uc+davis&t=a&o=&pp=250', header=0)[0]
print(type(frame))
- Find out the most frequent program that people reported in this page.
Answer
program = frame['Program (Season)'].value_counts().head(1)
print(program)
- Try the whole process on all the inputs in the database. (Hint: return all the DataFrame into a list and then use pd.concat(dataframe_list). Be careful this method will fuse the DataFrames with their headers. Think how to solve this.)
I want to bake a cake for my friend's birthday. I found this API with lots of recipes, and I want to find a recipe for a cake with nutella in it. However, my friend is allergic to eggs, so I can't use any recipes that include eggs.
Please find recipes (and their URLs) of cakes that won't make my friend sick!
Answer
url = "http://www.recipepuppy.com/api/"
parameters = {"i": "nutella", "q" : "cake"}
resp = requests.get(url, params=parameters)
data = resp.json()
searchreturn = data['results']
for all in searchreturn:
if 'egg' not in all['ingredients']:
print(all['title'] + " : " + all['href'])