2017-01-22 46 views
-1

このjsonオブジェクトを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: Mixing dicts with non-Series may lead to ambiguous ordering.

+1

は、あなたがしているコードを表示します使用して完全なエラーメッセージあなたがそれを使うときに得ることができます。 – BrenBarn

+1

例外を発生させるコードと一緒になっているエラーについて言及する必要があります。今のところ、JSON文字列は、あなたが何を望んでいるのか、なぜエラーが出るのかについての情報はありません。必要な情報で質問を編集してください –

答えて

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... 
+0

それは素晴らしいですが、どうすればイニングを拡大できますか?そこにはたくさんのデータがあります。 –

関連する問題