I would like to turn a json file into a dataframe, but I get the error:
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.
Can anyone help me?
The json file is as follows:
{
"testCaseResult": {
"timestamp": 1696257561,
"testCaseStatus": "Failed",
"result": "Found nullCount=2. It should be 0",
"testResultValue": [
{
"name": "nullCount",
"value": "2"
}
],
"testCaseFailureStatus": {
"testCaseFailureStatusType": "New",
"updatedAt": 1696257581779
}
}
}
the python code I used is as follows
import pandas as pd
f = open(fjson )
data = json.load(f)
f.close()
for i in data['testCaseResult']:
print(i)
testCaseResult_df = pd.DataFrame(data['testCaseResult'])
4
Answers
Your json contains nested structure. You have to flatten the JSON object before converting to dataframe.
You can use
json_normalize
:You have to think about what is Dataframe.
Dataframe of Pandas is for tabular data consisting of rows and columns.
The json attached by you seems not proper without preprocessing.
This error comes from the array in your json.
In my thingking, it is reasonable for the error to occur unless you modify the data.
The error you’re encountering typically occurs when you try to convert a nested dictionary structure into a Pandas DataFrame directly. To convert this JSON data into a DataFrame, you need to flatten the nested structure. You can use the code:
Here we recursively flatten the nested JSON structure and converting the flattened data into a Pandas DataFrame.