英文:
Creating a column containing the other columns as a JSON object?
问题
我正在尝试将一个包含其他列信息的列添加到我的数据框中,格式为json对象
我的数据框如下所示:
| col_1| col_2|
|:---- |:------:|
| 1| 1|
|2|2|
然后,我尝试使用以下代码添加json列
```python
for i, row in df.iterrows():
i_val = row.to_json()
df.at[i, 'raw_json'] = i_val
然而,结果是一个"级联"数据框,其中json出现两次
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}} |
我希望它看起来像以下这样
col_1 | col_2 | raw_json |
---|---|---|
1 | 1 | {"col_1":1,"col_2":1} |
2 | 2 | {"col_1":2,"col_2":2} |
<details>
<summary>英文:</summary>
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
```python
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} |
答案1
得分: 3
使用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}]
英文:
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}]
答案2
得分: 0
使用列表推导和itterrows
(如果您想要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}
英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论