0

So I have a script for my portfolio that has to do with NBA players and the nba_api. The NBA api uses an endpoint system, and the function I use (find_players_by_full_name) finds the player's by a user's input (hopefully the player's full name). I then grab the player ID and use it with other endpoints.

However, the problem here is, what if the endpoint return's multiple names? For example, the input ("Kyle") returns too many to count.

from nba_api.stats.library.parameters import SeasonAll
from nba_api.stats.endpoints import MatchupsRollup
player_dict = players.get_active_players()


find = input("Player Name")
findPlayer = players.find_players_by_full_name(find)

print(findPlayer)

#returns [{'id': 1629734, 'full_name': 'Kyle Alexander', 'first_name': 'Kyle', 'last_name': 'Alexander', 'is_active': False}, {'id': 203937, 'full_name': 'Kyle Anderson', 'first_name': 'Kyle', 'last_name': 'Anderson', 'is_active': True}, {'id': 1627858, 'full_name': 'Kyle Collinsworth', 'first_name': 'Kyle', 'last_name': 'Collinsworth', 'is_active': False}, {'id': 1629657, 'full_name': 'Kyle Guy', 'first_name': 'Kyle', 'last_name': 'Guy', 'is_active': True}, {'id': 2594, 'full_name': 'Kyle Korver', 'first_name': 'Kyle', 'last_name': 'Korver', 'is_active': False}, {'id': 1628398, 'full_name': 'Kyle Kuzma', 'first_name': 'Kyle', 'last_name': 'Kuzma', 'is_active': True}, {'id': 200768, 'full_name': 'Kyle Lowry', 'first_name': 'Kyle', 'last_name': 'Lowry', 'is_active': True}, {'id': 77438, 'full_name': 'Kyle Macy', 'first_name': 'Kyle', 'last_name': 'Macy', 'is_active': False}, {'id': 203124, 'full_name': "Kyle O'Quinn", 'first_name': 'Kyle', 'last_name': "O'Quinn", 'is_active': False}, {'id': 202713, 'full_name': 'Kyle Singler', 'first_name': 'Kyle', 'last_name': 'Singler', 'is_active': False}, {'id': 201602, 'full_name': 'Kyle Weaver', 'first_name': 'Kyle', 'last_name': 'Weaver', 'is_active': False}, {'id': 1627787, 'full_name': 'Kyle Wiltjer', 'first_name': 'Kyle', 'last_name': 'Wiltjer', 'is_active': False}]

I've managed to use pf = pd.DataFrame({'col':findPlayer}) and pf.to_csv('file_name.csv') to sort the names into neat rows.

,col
0,"{'id': 1629734, 'full_name': 'Kyle Alexander', 'first_name': 'Kyle', 'last_name': 'Alexander', 'is_active': False}"
1,"{'id': 203937, 'full_name': 'Kyle Anderson', 'first_name': 'Kyle', 'last_name': 'Anderson', 'is_active': True}"
2,"{'id': 1627858, 'full_name': 'Kyle Collinsworth', 'first_name': 'Kyle', 'last_name': 'Collinsworth', 'is_active': False}"
3,"{'id': 1629657, 'full_name': 'Kyle Guy', 'first_name': 'Kyle', 'last_name': 'Guy', 'is_active': True}"
4,"{'id': 2594, 'full_name': 'Kyle Korver', 'first_name': 'Kyle', 'last_name': 'Korver', 'is_active': False}"
5,"{'id': 1628398, 'full_name': 'Kyle Kuzma', 'first_name': 'Kyle', 'last_name': 'Kuzma', 'is_active': True}"
6,"{'id': 200768, 'full_name': 'Kyle Lowry', 'first_name': 'Kyle', 'last_name': 'Lowry', 'is_active': True}"
7,"{'id': 77438, 'full_name': 'Kyle Macy', 'first_name': 'Kyle', 'last_name': 'Macy', 'is_active': False}"
8,"{'id': 203124, 'full_name': ""Kyle O'Quinn"", 'first_name': 'Kyle', 'last_name': ""O'Quinn"", 'is_active': False}"
9,"{'id': 202713, 'full_name': 'Kyle Singler', 'first_name': 'Kyle', 'last_name': 'Singler', 'is_active': False}"
10,"{'id': 201602, 'full_name': 'Kyle Weaver', 'first_name': 'Kyle', 'last_name': 'Weaver', 'is_active': False}"
11,"{'id': 1627787, 'full_name': 'Kyle Wiltjer', 'first_name': 'Kyle', 'last_name': 'Wiltjer', 'is_active': False}"

However, now the problem is presenting all the names as a choice to the user, grabbing the user's selection and getting the player ID of said selection. How can I do that?

I hope my question has been detailed enough and I apologize if it isn't as it's my first time on Stackoverflow.



mabdali
  • 1
  • 2

0 Answers0