【问题标题】:TypeError: from_dict() got an unexpected keyword argument 'index'TypeError:from_dict() 得到了一个意外的关键字参数“索引”
【发布时间】:2019-04-27 16:21:44
【问题描述】:

我的 json 看起来像这样:

{
  "formatVersion" : "v1.0",
  "disclaimer" : "This pricing list is for informational purposes only ..."
  "offerCode" : "AmazonEC2",
  "version" : "20181122020351",
  "publicationDate" : "2018-11-22T02:03:51Z",
  "products" : {
    "G5FFNNK98ETA2UBE" : {
      "sku" : "G5FFNNK98ETA2UBE",
      "productFamily" : "Compute Instance",
      "attributes" : {
        "servicecode" : "AmazonEC2",
        "location" : "Asia Pacific (Tokyo)",
        "locationType" : "AWS Region",
        "instanceType" : "c4.4xlarge",
        "currentGeneration" : "Yes",
        "instanceFamily" : "Compute optimized",
        "vcpu" : "16",
        "physicalProcessor" : "Intel Xeon E5-2666 v3 (Haswell)",
        "clockSpeed" : "2.9 GHz",
        "memory" : "30 GiB",
        "storage" : "EBS only",

我正在尝试使用以下代码将其转换为 Pandas DataFrame:

df = pd.DataFrame()

for sku, data in json.loads(ec2offer)['products'].items():
    if data['productFamily'] == 'Compute Instance':
        new_df = pd.DataFrame.from_dict(data['attributes'], index=[0])
        df.append(new_df, ignore_index=True)

print(df)    

在添加index=[0] 之前,我收到了错误“ValueError: If using all scalar values, you must pass an index” 所以我根据Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index"的答案添加了这一点

现在我收到了这个错误:

TypeError: from_dict() 得到了一个意外的关键字参数 'index'

TL;DR

忘记上面的代码。将上述 json 中的每个“属性”结构添加到 Pandas 数据框中自己的行中的最简单方法是什么?

预期输出

instanceType   memory   ...
c4.4xlarge     30 Gib   ...
...            ...      ...

【问题讨论】:

  • 你能把这个添加到问题中吗?因为产品属性是一个嵌套的字典,不知道你想如何在输出df中显示它

标签: python pandas


【解决方案1】:
jsonstr={
"formatVersion": "v1.0",
"disclaimer": "This pricing list is for informational purposes only ...",
"offerCode": "AmazonEC2",
"version": "20181122020351",
"publicationDate": "2018-11-22T02:03:51Z",
"products": {
    "G5FFNNK98ETA2UBE": {
        "sku": "G5FFNNK98ETA2UBE",
        "productFamily": "Compute Instance",
        "attributes": {
            "servicecode": "AmazonEC2",
            "location": "Asia Pacific (Tokyo)",
            "locationType": "AWS Region",
            "instanceType": "c4.4xlarge",
            "currentGeneration": "Yes",
            "instanceFamily": "Compute optimized",
            "vcpu": "16",
            "physicalProcessor": "Intel Xeon E5-2666 v3 (Haswell)",
            "clockSpeed": "2.9 GHz",
            "memory": "30 GiB",
            "storage": "EBS only"
        }
    },
    "G5FFNNK98ETA2VIB": {
        "sku": "G5FFNNK98ETA2UBE",
        "productFamily": "Compute Instance",
        "attributes": {
            "servicecode": "AmazonEC22",
            "location": "Asia Pacific (Tokyo)",
            "locationType": "AWS Region",
            "instanceType": "c4.4xlarge",
            "currentGeneration": "Yes",
            "instanceFamily": "Compute optimized",
            "vcpu": "16",
            "physicalProcessor": "Intel Xeon E5-2666 v3 (Haswell)",
            "clockSpeed": "2.9 GHz",
            "memory": "30 GiB",
            "storage": "EBS only"
        }
    }
}

}

import pandas as pd
d={}
for product in jsonstr['products'].keys():
   d[product]={}
   d[product]=jsonstr['products'][product]['attributes']
df=pd.DataFrame(d).T.reset_index().drop('index',1)

输出:

df

【讨论】:

  • @AlexR 这是预期的输出,所有属性都作为单独的列
  • @AlexR,检查多个产品的更新解决方案
  • @AlexR 如果它有助于解决这个问题,也接受答案。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-09-17
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 2021-03-17
  • 2020-10-01
相关资源
最近更新 更多