Pandas DataFrame Combine Certain Columns in Nested JSON

tgordon18

Let's say you have a pandas DataFrame that looks like this:

A1a  A1b   A2a    A2b   …   B1a …
0.25 0.75  0.10   0.5       1   
…    …     …      …         …   

And you want to output a JSON list of objects (one object for each row) that looks like this:

[
    {
        A: {
            1: {
                a: 0.25,
                b: 0.75
            },
            2: {
                a: 0.1,
                b: 0.5,
                ...
            },
            ...
        },
        B: {
            1: {
                a: 1
            },
            ...
        },
        ...
    },
    ...
]

What's the best way to do this?

There are obviously a lot of pandas/nested JSON questions on here, but I think this is different because I'm trying to nest specific columns within the same row, rather than grouping rows that have the same values in columns (like in this example).

BENY

Since you link the page , I will borrow the recur_dictify function from the accepted answer in that link

#make your df columns become multiple index 
df.columns=pd.MultiIndex.from_tuples(df.columns.map(list).map(tuple))

      A
      1          2
      a     b    a    b
0  0.25  0.75  0.1  0.5

#Then we apply the function
recur_dictify(df.T.reset_index())

{'A': {'1': {'a': 0.25, 'b': 0.75}, '2': {'a': 0.1, 'b': 0.5}}} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combine Pandas columns into a nested list

Combine Pandas DataFrame DateTime Columns

Combine two columns of pandas dataframe

Reorder certain columns in pandas dataframe

Combine multiple dataframes by summing certain columns in Pandas

Split out nested json/dictionary from Pandas dataframe into separate columns

Get nested JSON from pandas dataframe grouped by multiple columns

Flatten nested JSON (has multiple list) into multiple pandas dataframe columns

Combine columns in a Pandas DataFrame to a column of lists in a DataFrame

Pandas Dataframe to Nested JSON

Nested Json in to dataframe (pandas)

Flatten nested pandas dataframe columns

pandas combine nested dataframes into one single dataframe

Dataframe group columns to nested json

How to combine two columns of text in pandas dataframe

How to combine numeric columns in pandas dataframe with NaN?

Combine two columns in pandas dataframe but in specific order

Pandas: Combine two dataframe columns in a sorted column

Combine MultiIndex columns to a single index in a pandas dataframe

Combine columns of different types in Pandas Dataframe

Combine values of columns and state condition in Pandas Dataframe

Pandas PIVOT Dataframe and combine columns names by levels

Pandas: Pivot dataframe with text and combine columns

How to combine 2 columns in pandas DataFrame?

Flatten nested JSON columns in Pandas

Sum of only certain columns in a pandas Dataframe

Format certain floating dataframe columns into percentage in pandas

Format certain floating dataframe columns into percentage in pandas

How to exclude certain columns of a pandas dataframe?