我从yaml文件创建了这个json对象。我试图序列化它,但我收到错误。
{'info': {'city': 'Southampton',
'dates': [datetime.date(2005, 6, 13)],
'gender': 'male',
'match_type': 'T20',
'outcome': {'by': {'runs': 100}, 'winner': 'England'},
'overs': 20,
'player_of_match': ['KP Pietersen'],
'teams': ['England', 'Australia'],
'toss': {'decision': 'bat', 'winner': 'England'},
'umpires': ['NJ Llong', 'JW Lloyds'],
'venue': 'The Rose Bowl'},
'innings': [{'1st innings': {'deliveries': [{0.1: {'batsman': 'ME Trescothick',
'bowler': 'B Lee',
'non_striker': 'GO Jones',
'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
{'19.6': {'batsman': 'PD Collingwood',
'bowler': 'GD McGrath',
'non_striker': 'J Lewis',
'runs': {'batsman': 0, 'extras': 0, 'total': 0},
'wicket': {'fielders': ['RT Ponting'],
'kind': 'caught',
'player_out': 'PD Collingwood'}}}],
'team': 'England'}},
{'2nd innings': {'deliveries': [{'0.1': {'batsman': 'AC Gilchrist',
'bowler': 'D Gough',
'non_striker': 'ML Hayden',
'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
{'14.3': {'batsman': 'GD McGrath',
'bowler': 'SJ Harmison',
'non_striker': 'MS Kasprowicz',
'runs': {'batsman': 0, 'extras': 0, 'total': 0},
'wicket': {'kind': 'bowled', 'player_out': 'GD McGrath'}}}],
'team': 'Australia'}}]}
非常感谢任何帮助。我试图将这个json对象转换为csv文件中的一行。
我正在尝试运行的代码:
df=pandas.read_json(cric_match_json) # reading json object as pandas dataframe
df.to_csv()
错误
ValueError:将dicts与非Series混合可能会导致模糊排序。
答案 0 :(得分:1)
IIUC您可以使用json_normalize()方法:
In [76]: d = {'info': {'city': 'Southampton',
...: 'dates': [datetime.date(2005, 6, 13)],
...: 'gender': 'male',
...: 'match_type': 'T20',
...: 'outcome': {'by': {'runs': 100}, 'winner': 'England'},
...: 'overs': 20,
...: 'player_of_match': ['KP Pietersen'],
...: 'teams': ['England', 'Australia'],
...: 'toss': {'decision': 'bat', 'winner': 'England'},
...: 'umpires': ['NJ Llong', 'JW Lloyds'],
...: 'venue': 'The Rose Bowl'},
...: 'innings': [{'1st innings': {'deliveries': [{0.1: {'batsman': 'ME Trescothick',
...: 'bowler': 'B Lee',
...: 'non_striker': 'GO Jones',
...: 'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
...: {'19.6': {'batsman': 'PD Collingwood',
...: 'bowler': 'GD McGrath',
...: 'non_striker': 'J Lewis',
...: 'runs': {'batsman': 0, 'extras': 0, 'total': 0},
...: 'wicket': {'fielders': ['RT Ponting'],
...: 'kind': 'caught',
...: 'player_out': 'PD Collingwood'}}}],
...: 'team': 'England'}},
...: {'2nd innings': {'deliveries': [{'0.1': {'batsman': 'AC Gilchrist',
...: 'bowler': 'D Gough',
...: 'non_striker': 'ML Hayden',
...: 'runs': {'batsman': 0, 'extras': 0, 'total': 0}}},
...: {'14.3': {'batsman': 'GD McGrath',
...: 'bowler': 'SJ Harmison',
...: 'non_striker': 'MS Kasprowicz',
...: 'runs': {'batsman': 0, 'extras': 0, 'total': 0},
...: 'wicket': {'kind': 'bowled', 'player_out': 'GD McGrath'}}}],
...: 'team': 'Australia'}}]}
In [77]: pd.io.json.json_normalize(d)
Out[77]:
info.city info.dates info.gender info.match_type info.outcome.by.runs info.outcome.winner info.overs \
0 Southampton [2005-06-13] male T20 100 England 20
info.player_of_match info.teams info.toss.decision info.toss.winner info.umpires \
0 [KP Pietersen] [England, Australia] bat England [NJ Llong, JW Lloyds]
info.venue innings
0 The Rose Bowl [{'1st innings': {'de...