I was using pandas to convert the NBA API JSON format to csv, and am getting a traceback error. Now I will note I ran into another traceback error a few days ago, but this one is different.
This error is troubling me in particularly because the same code worked earlier in the program. Here is the error in the console: Traceback (most recent call last):
File "nbagamestats.py", line 48, in <module>
dfLoop.columns = headersLoop
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\generic.py", line 5149, in __setattr__
return object.__setattr__(self, name, value)
File "pandas\_libs\properties.pyx", line 66, in pandas._libs.properties.AxisProperty.__set__
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\generic.py", line 564, in _set_axis
self._mgr.set_axis(axis, labels)
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\internals\managers.py", line 226, in set_axis
raise ValueError(
ValueError: Length mismatch: Expected axis has 0 elements, new values have 24 elements
Also, here is all the code I've written:
from nba_api.stats.endpoints import boxscoretraditionalv2
from nba_api.stats.endpoints import shotchartdetail
import pandas as pd
import json
from openpyxl import Workbook
print('Game ID?')
gamenum = input()
### PART ONE - Player List###
response = boxscoretraditionalv2.BoxScoreTraditionalV2(
end_period = 0,
game_id= gamenum
)
content = json.loads(response.get_json())
# transform contents into dataframe
results = content['resultSets'][0]
headers = results['headers']
rows = results['rowSet']
df = pd.DataFrame(rows)
df.columns = headers
playerList = df['PLAYER_ID'].tolist()
print(playerList)
###PART TWO - Download Player Shotchart Data###
for i in playerList :
filename = str(i) +'.xlsx'
responseLoop = shotchartdetail.ShotChartDetail(
team_id= 0,
#last_n_games = numGames,
game_id_nullable = gamenum,
player_id= i
)
contentLoop = json.loads(responseLoop.get_json())
# transform contents into dataframe
resultsLoop = contentLoop['resultSets'][0]
headersLoop = resultsLoop['headers']
rowsLoop = resultsLoop['rowSet']
dfLoop = pd.DataFrame(rowsLoop)
dfLoop.columns = headersLoop
# write to excel file
dfLoop.to_excel(filename, index=False)
print(i + ' has been written')
Hopefully, yall can help, thanks.