Python: from csv to APi

NBA Player Value

Python: From csv to plot via

There are a few tutorials out there that outline importing from a csv to python, and some use that to a different end but I wanted to join the two into a fairly easy walk-through. Coming from the R environment I was not sold on python because of the limited visualization capabilities. Another point is that it does not seem, in my mind, as intuitive as the R syntax. The craziest part of the Python world is summed up in the Python documentation:

In plain English: String literals can be enclosed in matching single quotes (‘) or double quotes (“).

Interchangeable quotes? Bonkers. Anyways to start you can download this csv of NBA player data.


Just some NBA player data.

Step 1

We make this handy function to read csv data. One thing to point out is the part, ‘rU’. There is a common error with Python about quoted values and this is the universal newline expression to solve them.

import csv
def getColumn(filename, column):
 results = csv.reader(open(filename, 'rU'), delimiter=",")
 return [result[column] for result in results]

Then we assign we name the pieces (columns) what we want. As you see the getColumn is our function we just defined, the players.csv is our csv in our workspace, and the number is the column number that corresponds to the ones we want.

player = getColumn("players.csv",2)
team = getColumn("players.csv",1)
value = getColumn("players.csv",22)
points = getColumn("players.csv",21)

Step 2

We use our api key information we get from our account, which you can find in the api section of your account if you log in. You can actually copy paste this section from itself because it fills in the ids and your credentials for you! (handy)

! python -c "import plotly;'yourusernamehere', api_key='yourapikeyhere', stream_ids=[stream1id', 'stream2id'])"
import plotly.plotly as py
from plotly.graph_objs import *

Step 3

Finally we can plot.

trace1 = Scatter(
 marker=Marker(color='rgb(234, 153, 153)')
trace2 = Bar(
 name='Model Value',
 marker=Marker(color='rgb(55, 83, 109)')
layout = Layout(
 title='NBA Player Value',
data = Data([trace1, trace2])
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='Player Value')

There is a lot here but if you view the APi documentation it all makes sense. There are also example plots you can use as a reference. I hope this helps somewhere, the thing I found most helpful was the simple function to import csv values.

As always I appreciate your feedback.