如何创建变量来循环文件并出现在数据帧中?

2024-09-24 22:22:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个数据框,其中包含特定球员“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

Tags: 文件数据fromimportjsonaswithopen
1条回答
网友
1楼 · 发布于 2024-09-24 22:22:29

好吧,这似乎是如此基本的知识,我不明白你为什么要问这个

#  - before loop  -

tennisstats = []

#  - loop  -

for filename in ["lenny/2016/lenny2016_match (1).json", "lenny/2016/lenny2016_match (2).json"]: 

    with open(filename) as json_file:
         lennymatch = json.load(json_file)

    player = [item 
         for item in lennymatch["stats"] 
         if item["player_fullname"] == "Lenny Hampel"]

    tabele = pd.DataFrame.from_dict(player)

    tennisstats.append(tabele)

#  - after loop  -

result = pd.concat(tennisstats)

如果文件名相似,但编号不同

for number in range(1, 101):

    filename = f"lenny/2016/lenny2016_match ({number}).json"

    with open(filename) as json_file:

其余部分与第一版相同


如果所有文件都在同一个文件夹中,那么您可能应该使用os.listdir()

directory = "lenny/2016/"

for name in os.listdir(directory):

    filename = directory + name

    with open(filename) as json_file:

其余部分与第一版相同

相关问题 更多 >