创建一个包含其他列的列,作为一个JSON对象?

huangapple go评论66阅读模式
英文:

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&#39;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&#39;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,&#39;raw_json&#39;] = 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=&#39;records&#39;)

df[&#39;raw_json&#39;] = df.to_json(orient=&#39;records&#39;)


   col_1  col_2                                       raw_json
0      1      1  [{&quot;col_1&quot;:1,&quot;col_2&quot;:1},{&quot;col_1&quot;:2,&quot;col_2&quot;:2}]
1      2      2  [{&quot;col_1&quot;:1,&quot;col_2&quot;:1},{&quot;col_1&quot;:2,&quot;col_2&quot;: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[&quot;raw_json&quot;] = [pd.DataFrame(data=[row], columns=df.columns).to_dict(orient=&quot;records&quot;)[0] for _, row in df.iterrows()]
print(df)

Output:

   col_1  col_2                  raw_json
0      1      1  {&#39;col_1&#39;: 1, &#39;col_2&#39;: 1}
1      2      2  {&#39;col_1&#39;: 2, &#39;col_2&#39;: 2}

huangapple
  • 本文由 发表于 2023年2月18日 07:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定