Who are My Friends in League of Legends? Compiling User Data

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 requests
import pandas as pd
import json

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.

names.append(“grumpyII”)

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/&#8221; + 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!

print friends

Screen Shot 2015-03-21 at 10.47.23 AM

Data frame of users in list “names”

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!

Advertisements

One thought on “Who are My Friends in League of Legends? Compiling User Data

  1. Pingback: Working with Nested Dictionaries in Python | Alyssa Fu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s