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条答案
按热度按时间cbeh67ev1#
使用
df.to_json(orient='records')
vlju58qv2#
使用一个列表comp和itterrows(你的预期有一个dict,如果你想要json,你可以删除[0]):
输出: