栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将Pandas Dataframe转换为嵌套JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Pandas Dataframe转换为嵌套JSON

更新:

j = (df.groupby(['ID','Location','Country','Latitude','Longitude'], as_index=False)  .apply(lambda x: x[['timestamp','tide']].to_dict('r'))  .reset_index()  .rename(columns={0:'Tide-Data'})  .to_json(orient='records'))

结果(格式化):

In [103]: print(json.dumps(json.loads(j), indent=2, sort_keys=True))[  {    "Country": "FRA",    "ID": 1,    "Latitude": 48.383,    "Location": "BREST",    "Longitude": -4.495,    "Tide-Data": [      {        "tide": 6905.0,        "timestamp": "1807-01-01"      },      {        "tide": 6931.0,        "timestamp": "1807-02-01"      },      {        "tide": 6896.0,        "timestamp": "1807-03-01"      },      {        "tide": 6953.0,        "timestamp": "1807-04-01"      },      {        "tide": 7043.0,        "timestamp": "1807-05-01"      }    ]  },  {    "Country": "DEU",    "ID": 7,    "Latitude": 53.867,    "Location": "CUXHAVEN 2",    "Longitude": 8.717,    "Tide-Data": [      {        "tide": 7093.0,        "timestamp": "1843-01-01"      },      {        "tide": 6688.0,        "timestamp": "1843-02-01"      },      {        "tide": 6493.0,        "timestamp": "1843-03-01"      },      {        "tide": 6723.0,        "timestamp": "1843-04-01"      },      {        "tide": 6533.0,        "timestamp": "1843-05-01"      }    ]  },  {    "Country": "DEU",    "ID": 8,    "Latitude": 53.899,    "Location": "WISMAR 2",    "Longitude": 11.458,    "Tide-Data": [      {        "tide": 6957.0,        "timestamp": "1848-07-01"      },      {        "tide": 6944.0,        "timestamp": "1848-08-01"      },      {        "tide": 7084.0,        "timestamp": "1848-09-01"      },      {        "tide": 6898.0,        "timestamp": "1848-10-01"      },      {        "tide": 6859.0,        "timestamp": "1848-11-01"      }    ]  },  {    "Country": "NLD",    "ID": 9,    "Latitude": 51.918,    "Location": "MAASSLUIS",    "Longitude": 4.25,    "Tide-Data": [      {        "tide": 6880.0,        "timestamp": "1848-02-01"      },      {        "tide": 6700.0,        "timestamp": "1848-03-01"      },      {        "tide": 6775.0,        "timestamp": "1848-04-01"      },      {        "tide": 6580.0,        "timestamp": "1848-05-01"      },      {        "tide": 6685.0,        "timestamp": "1848-06-01"      }    ]  },  {    "Country": "USA",    "ID": 10,    "Latitude": 37.807,    "Location": "SAN FRANCISCO",    "Longitude": -122.465,    "Tide-Data": [      {        "tide": 6909.0,        "timestamp": "1854-07-01"      },      {        "tide": 6940.0,        "timestamp": "1854-08-01"      },      {        "tide": 6961.0,        "timestamp": "1854-09-01"      },      {        "tide": 6952.0,        "timestamp": "1854-10-01"      },      {        "tide": 6952.0,        "timestamp": "1854-11-01"      }    ]  }]

旧答案:

你可以用它做的

groupby()
apply()
to_json()
方法:

j = (df.groupby(['ID','Location','Country','Latitude','Longitude'], as_index=False)       .apply(lambda x: dict(zip(x.timestamp,x.tide)))       .reset_index()       .rename(columns={0:'Tide-Data'})       .to_json(orient='records'))

输出:

In [112]: print(json.dumps(json.loads(j), indent=2, sort_keys=True))[  {    "Country": "FRA",    "ID": 1,    "Latitude": 48.383,    "Location": "BREST",    "Longitude": -4.495,    "Tide-Data": {      "1807-01-01": 6905.0,      "1807-02-01": 6931.0,      "1807-03-01": 6896.0,      "1807-04-01": 6953.0,      "1807-05-01": 7043.0    }  },  {    "Country": "DEU",    "ID": 7,    "Latitude": 53.867,    "Location": "CUXHAVEN 2",    "Longitude": 8.717,    "Tide-Data": {      "1843-01-01": 7093.0,      "1843-02-01": 6688.0,      "1843-03-01": 6493.0,      "1843-04-01": 6723.0,      "1843-05-01": 6533.0    }  },  {    "Country": "DEU",    "ID": 8,    "Latitude": 53.899,    "Location": "WISMAR 2",    "Longitude": 11.458,    "Tide-Data": {      "1848-07-01": 6957.0,      "1848-08-01": 6944.0,      "1848-09-01": 7084.0,      "1848-10-01": 6898.0,      "1848-11-01": 6859.0    }  },  {    "Country": "NLD",    "ID": 9,    "Latitude": 51.918,    "Location": "MAASSLUIS",    "Longitude": 4.25,    "Tide-Data": {      "1848-02-01": 6880.0,      "1848-03-01": 6700.0,      "1848-04-01": 6775.0,      "1848-05-01": 6580.0,      "1848-06-01": 6685.0    }  },  {    "Country": "USA",    "ID": 10,    "Latitude": 37.807,    "Location": "SAN FRANCISCO",    "Longitude": -122.465,    "Tide-Data": {      "1854-07-01": 6909.0,      "1854-08-01": 6940.0,      "1854-09-01": 6961.0,      "1854-10-01": 6952.0,      "1854-11-01": 6952.0    }  }]

PS,如果您不关心标识,则可以直接写入JSON文件:

(df.groupby(['ID','Location','Country','Latitude','Longitude'], as_index=False)   .apply(lambda x: dict(zip(x.timestamp,x.tide)))   .reset_index()   .rename(columns={0:'Tide-Data'})   .to_json('/path/to/file_name.json', orient='records'))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号