skip to Main Content

I’m trying add a column to my dataframe that contains the information from the other columns as a json object

My dataframe looks like this:

col_1 col_2
1 1
2 2

I’m then trying to add the json column using the following

    for i, row in df:
        i_val = row.to_json()
        df.at[i,'raw_json'] = i_val

However it results in a "cascaded" dataframe where the json appears twice

col_1 col_2 raw_json
1 1 {"col_1":1,"col_2":1,"raw_json":{"col_1":1,"col_2":1}}
2 2 {"col_1":2,"col_2":2,"raw_json":{"col_1":2,"col_2":2}}

I’m expecting it to look like the following

col_1 col_2 raw_json
1 1 {"col_1":1,"col_2":1}
2 2 {"col_1":2,"col_2":2}

2

Answers


  1. use df.to_json(orient='records')

    df['raw_json'] = df.to_json(orient='records')
    
    
       col_1  col_2                                       raw_json
    0      1      1  [{"col_1":1,"col_2":1},{"col_1":2,"col_2":2}]
    1      2      2  [{"col_1":1,"col_2":1},{"col_1":2,"col_2":2}]
    
    Login or Signup to reply.
  2. Using a list comp and itterrows (your expected has a dict if you want json you can remove the [0]):

    df["raw_json"] = [pd.DataFrame(data=[row], columns=df.columns).to_dict(orient="records")[0] for _, row in df.iterrows()]
    print(df)
    

    Output:

       col_1  col_2                  raw_json
    0      1      1  {'col_1': 1, 'col_2': 1}
    1      2      2  {'col_1': 2, 'col_2': 2}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search