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

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

Creating a column containing the other columns as a JSON object?

问题

  1. 我正在尝试将一个包含其他列信息的列添加到我的数据框中格式为json对象
  2. 我的数据框如下所示
  3. | col_1| col_2|
  4. |:---- |:------:|
  5. | 1| 1|
  6. |2|2|
  7. 然后我尝试使用以下代码添加json
  8. ```python
  9. for i, row in df.iterrows():
  10. i_val = row.to_json()
  11. 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}
  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying add a column to my dataframe that contains the information from the other columns as a json object
  4. My dataframe looks like this:
  5. | col_1| col_2|
  6. |:---- |:------:|
  7. | 1| 1|
  8. |2|2|
  9. I&#39;m then trying to add the json column using the following
  10. ```python
  11. for i, row in df:
  12. i_val = row.to_json()
  13. 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')

  1. df['raw_json'] = df.to_json(orient='records')
  2. col_1 col_2 raw_json
  3. 0 1 1 [{"col_1":1,"col_2":1},{"col_1":2,"col_2":2}]
  4. 1 2 2 [{"col_1":1,"col_2":1},{"col_1":2,"col_2":2}]
英文:

use df.to_json(orient=&#39;records&#39;)

  1. df[&#39;raw_json&#39;] = df.to_json(orient=&#39;records&#39;)
  2. col_1 col_2 raw_json
  3. 0 1 1 [{&quot;col_1&quot;:1,&quot;col_2&quot;:1},{&quot;col_1&quot;:2,&quot;col_2&quot;:2}]
  4. 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]中的字典部分):

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

输出:

  1. col_1 col_2 raw_json
  2. 0 1 1 {'col_1': 1, 'col_2': 1}
  3. 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]):

  1. df[&quot;raw_json&quot;] = [pd.DataFrame(data=[row], columns=df.columns).to_dict(orient=&quot;records&quot;)[0] for _, row in df.iterrows()]
  2. print(df)

Output:

  1. col_1 col_2 raw_json
  2. 0 1 1 {&#39;col_1&#39;: 1, &#39;col_2&#39;: 1}
  3. 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:

确定