将包含其他列的列创建为JSON对象?

dgiusagp  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(129)

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} |

cbeh67ev

cbeh67ev1#

使用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}]
vlju58qv

vlju58qv2#

使用一个列表comp和itterrows(你的预期有一个dict,如果你想要json,你可以删除[0]):

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

输出:

col_1  col_2                  raw_json
0      1      1  {'col_1': 1, 'col_2': 1}
1      2      2  {'col_1': 2, 'col_2': 2}

相关问题