Home > Back-end >  How do I convert this JSON to DataFrame?
How do I convert this JSON to DataFrame?

Time:12-20

I have a JSON that I am struggling to convert into a Python DataFrame. The JSON takes the following form:

{
    "chart_data": [
        {
            "date": 1639872000,
            "arrivals": 80,
            "departures": 79
        },
        {
            "date": 1639785600,
            "arrivals": 80,
            "departures": 78
        },
        {
            "date": 1639699200,
            "arrivals": 78,
            "departures": 77
        },
        {
            "date": 1639612800,
            "arrivals": 78,
            "departures": 77
        },

Ultimately, I want to achieve the following table:

Date Arrivals Departures
1639872000 80 79
1639785600 80 78

I have tried pd.read_json() but I always get an error "Mixing dicts with non-Series may lead to ambiguous ordering."

CodePudding user response:

Try to explore pd.DataFrame.from_records(...)

e.g.:

pd.DataFrame.from_records(data.chart_data, columns=['date', 'arrivals', 'departures'])

CodePudding user response:

You can implement a dataframe in this way for your use case:

pd.DataFrame(x["chart_data"])

As the initial 'chart_data' object holds all the data you want the DataFrame to store.

  • Related