For my first foray into the Python, API, JSON world, I decided to use League of Legends (LoL) data and see if I could call it, manipulate it, and make some use of it!
My goal for this task was to pull League of Legends data on specific users (using the API) and assemble that information (formatted with JSON) into a table, in this case, a data frame using Python.
In my other post, I described the general process of accessing the League of Legends API, so I won’t go over that here. Instead, I’ll talk about the specific steps I took to create a Python object containing a list of selected friends and their associated general user info from LoL. So the following script assumes you know how to pull data off of the League of Legends API.
Import the libraries you’ll need to run import data from a URL (request), read JSON data (json), and create a data frame (pandas). Importing pandas as pd allows for easy reference to functions in pandas.
import pandas as pd
Create a list of the names you wish to pull (your League of Legends friends!).
names = [“kallyope”, “kallykallyope”, “zunger”, “dowentz”, “dead peon”]
If you want to add anyone to your names list, use the .append() call.
Create an empty data frame for your friends.
friends = pd.DataFrame()
The pd lets Python know that you are pulling the call .DataFrame() from the library pandas (pd).
Create a for loop to pull each name from your names list that does the following:
(1) Gets the URL associated with that name (which calls the League of Legends profile information of the friend from their API)
(2) Reads the URL into Python
(3) Unloads the JSON data into a readable format in Python
(4) Creates a data frame for that name’s data (that is transposed, since the default will make the keys the index of the data frame, and the values the first column. EDIT: I just learned, this is only the case for nested dict objects, which this one is.)
(5) Appends that name’s data to the overall data frame, friends.
<key> is a specific API key that League of Legends generates for you. You should replace this with your own API key. Don’t share this with anyone!
for name in names:
url = “https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/” + str(name) + “?api_key=<key>”
resp = requests.get(url)
data = json.loads(resp.text)
user = pd.DataFrame(data).T
friends = pd.DataFrame.append(friends, user)
Print the data frame friends to see what it looks like!
Awesome!! Now I want to figure out how to pull down different kinds of data (e.g., win rate, items bought, time of deaths in game, time of CS in game if the API provides that information), manipulate the data, and run analyses on the data (Am I more likely to win games where my CS is high? Am I less likely to die when my CS is high? Am I more likely to win games when the jungler ganks the lanes?).
I also want to figure out how to link up this data to the web and create an interactive webpage. For example, it would be cool to create a webpage based on this table I just made where you could input your friends’ names in a field, and the page will create a table that grows as you add more friends to it. It would be neat!