使用 Pandas 转换为 Json 格式时的回溯

Traceback when using Pandas to convert to Json Format

最近一直在尝试用NBAAPI拉投篮图数据。我将 link 我正在使用的特定功能的文档 here

我不断得到如下回溯:

Traceback (most recent call last):
  File "nbastatsrecieve2.py", line 27, in <module>
    df.to_excel(filename, index=False)
  File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\generic.py", line 2023, in to_excel
    formatter.write(
  File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\formats\excel.py", line 730, in write
    writer = ExcelWriter(stringify_path(writer), engine=engine)
  File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\excel\_base.py", line 637, in __new__
    raise ValueError(f"No engine for filetype: '{ext}'") from err
ValueError: No engine for filetype: ''

这是我目前拥有的所有代码:

from nba_api.stats.endpoints import shotchartdetail
import pandas as pd
import json

print('Player ID?')
playerid = input()
print('File Name?')
filename = input()

response = shotchartdetail.ShotChartDetail(
    team_id= 0,
    player_id= playerid
)

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

# write to excel file
df.to_excel(filename, index=False)

希望有人能提供帮助,因为我对 JSON 格式还很陌生。

你得到这个是因为 filename 没有扩展名。 Pandas 将使用文件名的扩展名(如 xlsxxls)(如果你没有给它一个 ExcelWriter)来理解为此使用的正确库格式。只需用 df.to_excel('filename.xlsx', index=False) 之类的东西试试这个,看看效果如何。