首页 > 解决方案 > 数据帧到 JSON 的转换

问题描述

我有一个需要转换为 JSON 的数据框。数据目前看起来像

                       text     ids
0                 add a car     None
1                         None  695f1
2                         None  a86b5c
3  add another car to my log    None
4                         None  1ba0
5                  Concerts     None
6                         None  a4f7
7                         None  fea
8                         None  410

我需要 JSON dict 看起来像这样并忽略 none

{
  "text": "add a car",
  "ids": [
    "695f1",
    "a86b5c"
  ]
}

我的步骤是:

弄清楚了。

首先设置 NaN 值,其中 None

df1 = df1.fillna(value=np.nan)

用先前已知值填充 NaN

df1['text'] = df['text'].fillna(method='ffill')

删除 NaN

df1 = df1.dropna()

转换为 json

df1.to_json('temp.json', orient='records', lines=True)

我遇到的问题是格式不正确。我在看

{"text":"add a car","ids":" 695f1"}
{"text":"add a car","ids":"a86b5c"}
{"text":"add another car to my log","ids":"1ba0"}
{"text":"Concerts","ids":"a4f7"}

我想 :

    {
  "text": "add a car", 
  "ids": 
    [
      "695f1", 
      "a86b5c"
    ]
  
}
{
  "text": "add another car to my log", 
  "ids": 
    [
      "1ba0", 
    ], 
 
}
{
"text": "Concerts", 
  "ids": 
    [
      "a4f7", 
      "fea",
      "410",
    ]
}

标签: jsonpandasdataframe

解决方案


我认为你很接近,list首先需要聚合:

df['text'] = df['text'].ffill()
df = df.dropna()

df1 = df.groupby('text', sort=False).agg(list).reset_index()
print (df1)
                        text               ids
0                  add a car   [695f1, a86b5c]
1  add another car to my log            [1ba0]
2                   Concerts  [a4f7, fea, 410]

df1.to_json('temp.json', orient='records')

推荐阅读