我想创建一个数据框,其中包含特定球员“Lenny Hampel”网球比赛的数据。为此,我下载了很多.json文件,其中包含了他比赛的数据——总共大约有100个文件。由于它是一个json文件,我需要将每个文件转换为dict,以便最终将其放入数据帧。最后,我需要将每个文件连接到数据帧。我可以硬编码,但是我认为这有点傻,但是我找不到一个合适的方法来迭代
你能帮我理解如何创建一个循环或smth,以便以智能的方式编写代码吗
from bs4 import BeautifulSoup
import requests
import json
import bs4 as bs
import urllib.request
from urllib.request import Request, urlopen
import pandas as pd
import pprint
with open('lenny/2016/lenny2016_match (1).json') as json_file:
lennymatch1 = json.load(json_file)
player = [item
for item in lennymatch1["stats"]
if item["player_fullname"] == "Lenny Hampel"]
with open('lenny/2016/lenny2016_match (2).json') as json_file:
lennymatch2 = json.load(json_file)
player2 = [item
for item in lennymatch2["stats"]
if item["player_fullname"] == "Lenny Hampel"]
with open('lenny/2016/lenny2016_match (3).json') as json_file:
lennymatch3 = json.load(json_file)
player33 = [item
for item in lennymatch3["stats"]
if item["player_fullname"] == "Lenny Hampel"]
with open('lenny/2016/lenny2016_match (4).json') as json_file:
lennymatch4 = json.load(json_file)
player4 = [item
for item in lennymatch4["stats"]
if item["player_fullname"] == "Lenny Hampel"]
tabelle1 = pd.DataFrame.from_dict(player)
tabelle2 = pd.DataFrame.from_dict(player2)
tabelle3 = pd.DataFrame.from_dict(player33)
tabelle4 = pd.DataFrame.from_dict(player4)
tennisstats = [tabelle1, tabelle2, tabelle3, tabelle4]
result = pd.concat(tennisstats)
result
好吧,这似乎是如此基本的知识,我不明白你为什么要问这个
如果文件名相似,但编号不同
其余部分与第一版相同
如果所有文件都在同一个文件夹中,那么您可能应该使用
os.listdir()
其余部分与第一版相同
相关问题 更多 >
编程相关推荐