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
use
df.to_json(orient='records')
Using a list comp and itterrows (your expected has a dict if you want json you can remove the [0]):
Output: