NBA API: Looping a dictionary to get data frames - python

I am trying to get a full database of all active players career stats in the NBA.
I'm relatively new to Python and am trying to figure out a way to iterate a loop function by looking up the playerID for each PlayerCareerStat data frame. Ultimately I will summarize and group the data so its easier to read but I am trying to return a list of all players career stats by season.
I am able to use the players.get_active_players() endpoint to return a list of all players with their player_id: [1]: https://i.stack.imgur.com/BFxgv.png
With that, I am tryin to loop the Player_id through each data frame in the PlayerCareerStats() endpoint ... I think? Since the parameter for this endpoint requires a single player_id I can't seem to get all the players. Please see picture [1]: https://i.stack.imgur.com/skM8Y.png
Does anyone know how I might be able to get the output I am trying to find?

When you create a function, it will end when it reaches return. So your function returns the player id, and doesn't move on to the part where you are trying to pull the career stats.
So get rid of that return player['id'], and the function should run all the way through:
from nba_api.stats.static import players
from nba_api.stats.endpoints import playercareerstats
import pandas as pd
def get_nba_id():
nba_players = players.get_active_players()
for player in nba_players:
career_stats = playercareerstats.PlayerCareerStats(player_id=player['id'])
career_df = career_stats.get_data_frames()[0]
career_df['NAME'] = player['full_name']
print(career_df)

Related

Getting track's URI's from Spotify API using Spotipy?

I am working in collecting a data set that crossreferences a track's audio features and the Billboard's chart data set available on Kaggle. I am trying to get each song's URI in order to then get its audio features, and I defined the following function:
def get_track_uri(track_title, sp):
result = sp.search(track_title, type="track", limit=1)
if result['tracks']['total'] > 0:
track_uri = result['tracks']['items'][0]['uri']
return track_uri
else:
return None
and then it goes through the Billboard's 'song' column in order to create a new column with the URIs.
cleandf['uri'] = cleandf['song'].apply(lambda x: get_track_uri(x, sp))
So, I left it running for about 40 min and I noticed that it got stuck in a sleep method from Spotipy which I gathered was because I was making a lot of requests to the Spotify API. How can I be able to go around this if I'm trying to go through 50,000 rows? I could maybe make it wait between search queries but it will easily take what, 15 hours? Also, there probably is a way to directly get the audio features without me getting the URI's, but it still would need to go through all of the rows.

How to get a list of streets from OpenStreetMaps for a populated place in a given country?

I am trying to extract lists of street names by names of populated places from OpenStreetMap / Overpass using e.g. the following Python code:
import requests
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area
[name="Fulda"];
way(area)[highway][name];
out;
"""
query = overpass_query
response = requests.get(overpass_url, params={'data': query})
data = response.json()
In this way I can get more or less everything I need, but the problem is how to avoid the ambiguity with the place names: there is a city in Germany called Fulda and a number or places in the US. The code above returns streets in all of them. Is there a possibility to modify the query so that it filters one certain country, e.g. Germany?
(I find BTW that the learning curve of OpenStreetMap is made unnecessarily steep, and it is difficult to find examples in the whole of the internet.)

How to limit open orders request to one pair with kraken api (python)?

In order to make my code more efficient, I'm trying to limit my api request for open orders to one single pair. I can't figure out how to correctly use the input parameters.
I'm using python3 and the krakenex package (which I could replace if there is one which works better)
client = krakenex.API(<<key>>, <<secret>>)
data = {'pair': 'ADAEUR'}
open_ord = client.query_private(method='OpenOrders',data = data) ['result']
open_ord_ = list(open_ord.values())[0]
---> this unfortunately returns the open orders of all my pairs and not only the "ADAEUR".
I guess one needs to adapt the data parameters which I was not able to figure out...
Would be awesome if someone could help me.
Many Thanks in advance
According to the Kraken API docs, there is no data argument for the getOpenOrders endpoint, so this explains why your results are not filtered.
Two methods:
Using the pykrakenapi package that neatly wraps all output in a Pandas DataFrame:
import krakenex
from pykrakenapi import KrakenAPI
api = krakenex.API(<<key>>, <<secret>>)
connection = KrakenAPI(api)
pairs = ['ADAEUR', 'XTZEUR']
open_orders = connection.get_open_orders()
open_orders = open_orders[open_orders['descr_pair'].isin(pairs)]
print(open_orders)
Using only krakenex and filtering from the JSON output:
import krakenex
api = krakenex.API(<<key>>, <<secret>>)
pairs = ['ADAEUR', 'XTZEUR']
open_orders = api.query_private(method='OpenOrders')['result']['open']
open_orders = [(o, open_orders[o]) for o in open_orders if open_orders[o]['descr']['pair'] in pairs]
print(open_orders)
Both methods are written so they can filter one or multiple pairs.
Method 1 returns a Pandas DataFrame, the second method returns a list with for each open order a tuple of (order ID (str), order info (dict)).

Iterating Sequentially on the URL when running a Python API

I'm trying to pull data from an API for a fantasy football project I'm working on. You can pull data on various players using the url: 'https://fantasy.premierleague.com/api/element-summary/i/' where i is a number that relates to a player and runs from 1 to 400 or so.
I wrote code that pulls this data for a specific player and stores it as a dataframe for future analysis using the code:
import pandas as pd
import json
import requests
from pandas.io.json import json_normalize
r = requests.get('https://fantasy.premierleague.com/api/element-summary/1/')
r_json = r.json()
r_df = pd.DataFrame(r_json['history'])
r_df.head()
And this works great. The issue is it's only for 1 player and there are lots of them, so what I want is a DataFrame that contains this data for all of the players. I figured I could use a for loop for this but I can't get it to work. I'm trying the code:
import pandas as pd
import json
import requests
from pandas.io.json import json_normalize
for i in range(5):
r = requests.get('https://fantasy.premierleague.com/api/element-summary/{}/'.format(i))
r_json = r.json()
r_df= pd.DataFrame(r_json['history'])
r_df.head()
Where I've put the logic in a for loop, but I get the error:
KeyError: 'history'
When I try to run this. Why does it not like the line r_df= pd.DataFrame(r_json['history']) when it's in a for loop when it's OK outside of one?
Any help appreciated!
Thanks!
This is because your loop is trying to query 'https://fantasy.premierleague.com/api/element-summary/0/' on the first iteration, which doesn't exist. (Or, rather, gives you the JSON {"detail": "Not found."}, which doesn't have a "history" key.)
The built-in range function generates integers starting from zero by default. Try changing the loop range to range(1, 5) instead. (Also note that the end of the range is exclusive, so this will give you integers from 1 to 4.)
A KeyError indicates that the dictionary does not have this specific key.
Your specific problem here is that the range() function is generating the following range: [0, 1, 2, 3, 4] because if you don't specify an initial value, it will take 0 as an initial value and stop at one short of the final value.
And so https://fantasy.premierleague.com/api/element-summary/0/ doesn't return anything.
To fix this change range(5) to range(1, 6).
Note: the syntax of the range() function is: range(starting_point, ending_point, step)

Retrieve json store keys in a predetermined order in kivy?

Im working on a program using kivy and python, and i will be saving some data to a json file like say 'items.json'.
The thing is i intend to retrieve the data from the store and use them to form
a list of buttons in my app. here is an example.
store = JsonStore('items.json')
store.put('infinix', name = 'infinix', category = 'gadgets')
store.put('wrist watch', name = 'wrist watch', category = 'outfits')
store.put('t-shirt', name = 't-shirt', category = 'outfits')
this works well. But my problem is in retrieving the data.
i would like to get them in the same order i entered the data into the store.
for example if i do
store.keys()
i would like it to return
['infinix', 'wrist watch', 't-shirt']
which is the same order i entered the data.
currently whenever i try to retrieve the data, the order is mixed up.
is there a way to achieve what i need?
Any help is greatly appreciated.
The simplest option would seem to be just adding an extra storage key containing a list of your items in the correct order. You can then just check this first, and load them in that order.

Categories