sri lakshmi
sri lakshmi

Reputation: 47

Facing "Dataframe constructor not properly called" error while concat dataframe and json-normalize

When i try to concatenate a dataframe value and dictionary i am facing an error

Below is my sample json

{
 "creation-date": "Fri Mar 23 07:03:31 UTC 2018",
  "scan-with-high-privileges": true,
  "system-infos": {
    "hostname": "vmDiscovery",
    "domain": "aw4gb5ukuefulow5njy3bfktkc.rx.internal.cloudapp.net",
    "os": "",
    "os-details": {
      "kernel-version": "Linux vmDiscovery 3.10.0-693.17.1.el7.x86_64 #1 SMP Sun Jan 14 10:36:03 EST 2018 x86_64 x86_64 x86_64 GNU/Linux",
      "lsb-id": "",
      "lsb-version-compliance": "",
      "lsb-description": "",
      "lsb-release": "",
      "lsb-codename": ""
    },
    "cpu-count": 2,
    "cpu-name": "Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz",
    "total-memory": "4029248 kB",
    "hw-details": {
      "physical-virtual": "Virtual",
      "vendor": "Microsoft Corporation",
      "model": "7.0 (Virtual Machine)",
      "age": "06/02/2017"
    }
  },
}

I am trying to concatenate creation-date(first row) and a dictionary of system-infos.

below is the code:

import pandas as pd
import json
from pandas.io.json import json_normalize
from numpy.core.numeric import outer

with open("C:\\Users\\esrilka\\Documents\\jsonFiles\\jsonFiles\\Mynew.json") as fi:
    d = json.load(fi)
df = d['creation-date']
works_data = json_normalize(data=d['system-infos'], record_path=['os-details'], 
                            meta=['hostname', 'domain'])
result=pd.concat([works_data,df],axis=1)
result.to_csv("C:\\Users\\esrilka\\Documents\\jsonFiles\\sample.csv", index=False)

I am getting the below error:

Traceback (most recent call last):
  File "C:\Users\esrilka\eclipse-workspace\My First PyDev Project\Newsample.py", line 11, in <module>
    df = pd.DataFrame('creation-date')
  File "C:\Users\esrilka\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 404, in __init__
    raise ValueError('DataFrame constructor not properly called!')
ValueError: DataFrame constructor not properly called!

Upvotes: 1

Views: 2249

Answers (1)

Rakesh
Rakesh

Reputation: 82785

Remove df = pd.DataFrame('creation-date'). Directly use json_normalize to create your DF.

Ex:

import pandas as pd
import json
from pandas.io.json import json_normalize
d = {
 "creation-date": "Fri Mar 23 07:03:31 UTC 2018",
  "scan-with-high-privileges": True,
  "system-infos": {
    "hostname": "vmDiscovery",
    "domain": "aw4gb5ukuefulow5njy3bfktkc.rx.internal.cloudapp.net",
    "os": "",
    "os-details": {
      "kernel-version": "Linux vmDiscovery 3.10.0-693.17.1.el7.x86_64 #1 SMP Sun Jan 14 10:36:03 EST 2018 x86_64 x86_64 x86_64 GNU/Linux",
      "lsb-id": "",
      "lsb-version-compliance": "",
      "lsb-description": "",
      "lsb-release": "",
      "lsb-codename": ""
    },
    "cpu-count": 2,
    "cpu-name": "Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz",
    "total-memory": "4029248 kB",
    "hw-details": {
      "physical-virtual": "Virtual",
      "vendor": "Microsoft Corporation",
      "model": "7.0 (Virtual Machine)",
      "age": "06/02/2017"
    }
  },
}
df = json_normalize(data=d['system-infos'], record_path=['os-details'],
                            meta=['hostname', 'domain'])
print(df)

Updated snippet as per comments.

df = pd.DataFrame({"creation-date": [d["creation-date"]]})
df1 = json_normalize(data=d['system-infos'], record_path=['os-details'],meta=['hostname', 'domain'])
print(pd.concat([df, df1], axis=1))

Upvotes: 1

Related Questions