简体   繁体   English

使用json将文件读入pandas数据框

[英]Use json to read the file into a pandas data frame

I am trying to read the file into a pandas data frame, but get an error:我试图将文件读入 pandas 数据框,但出现错误:

ValueError: Mixing dicts with non-Series may lead to ambiguous ordering. ValueError:将字典与非系列混合可能会导致排序不明确。

Here is my code:这是我的代码:

%matplotlib inline

import pandas as pd
import numpy as np
import json

json_str = !cat data/commonTeamYears\?LeagueID\=00\&Season\=2018-19
json_obj = json.loads(json_str[0])
team=json.loads(json_str[0])
allteams=pd.DataFrame.from_dict(team)

That's I got for team:那是我为团队准备的:

{'resource': 'commonteamyears',
 'parameters': {'LeagueID': '00'},
 'resultSets': [{'name': 'TeamYears',
   'headers': ['LEAGUE_ID', 'TEAM_ID', 'MIN_YEAR', 'MAX_YEAR', 'ABBREVIATION'],
   'rowSet': [['00', 1610612737, '1949', '2019', 'ATL'],
    ['00', 1610612738, '1946', '2019', 'BOS'],
    ['00', 1610612740, '2002', '2019', 'NOP'],
    ['00', 1610612741, '1966', '2019', 'CHI'],
    ['00', 1610612742, '1980', '2019', 'DAL'],
    ['00', 1610612743, '1976', '2019', 'DEN'],
    ['00', 1610612745, '1967', '2019', 'HOU'],
    ['00', 1610612746, '1970', '2019', 'LAC'],
    ['00', 1610612747, '1948', '2019', 'LAL'],
    ['00', 1610612748, '1988', '2019', 'MIA'],
    ['00', 1610612749, '1968', '2019', 'MIL'],
    ...

According to pbpython with DataFrame.from_dict function you are creating the dataframe in a column oriented manner.根据带有DataFrame.from_dict DataFrame.from_dict的 pbpython,您正在以面向列的方式创建 dataframe。 Try DataFrame default constructor with a row oriented approach.使用面向行的方法尝试DataFrame默认构造函数。

allteams = pd.Dataframe(team)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM