美文网首页
python dataframe导出成json格式。

python dataframe导出成json格式。

作者: 丙吉 | 来源:发表于2020-04-10 10:27 被阅读0次

    df =pd.DataFrame([['a', 'b'], ['c', 'd']],

      index=['row 1', 'row 2'],

      columns=['col 1', 'col 2'])

    ###########

    split

    ###########

    df.to_json(orient='split')

    >'{"columns":["col 1","col 2"],

     "index":["row 1","row 2"],

     "data":[["a","b"],["c","d"]]}'

    ###########

    index

    ###########

    df.to_json(orient='index')

    >'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'

    ###########

    records

    ###########

    df.to_json(orient='index')

    >'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'

    ###########

    table

    ###########

    df.to_json(orient='table')

    >'{"schema": {"fields": [{"name": "index", "type": "string"},

      {"name": "col 1", "type": "string"},

      {"name": "col 2", "type": "string"}],

     "primaryKey": "index",

     "pandas_version": "0.20.0"},

     "data": [{"index": "row 1", "col 1": "a", "col 2": "b"},

     {"index": "row 2", "col 1": "c", "col 2": "d"}]}'

    相关文章

      网友评论

          本文标题:python dataframe导出成json格式。

          本文链接:https://www.haomeiwen.com/subject/nxvlmhtx.html