json - 数据帧到 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",
]
}
解决方案
我认为你很接近,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')
推荐阅读
- java - 将 WAR 部署到 Wildfly 14 时 commons-lang3 的 ClassNotFoundException
- java - Java-Stream-API:地图归约
- typescript - Not able to import class in angular 5
- java - 在 JavaFX XYSeries 中找到最大 Y
- struct - 如何制作一个所有字段都是公共的公共结构而不为每个字段重复“pub”?
- java - stuck with a malfunctioning while loop
- android - 如何在 React Native 中获取在线当前日期和时间
- prestashop - My Prestashop Back Office Is not lising payment methods
- python - How to disable vscode autocomplete when using a dot after an integer in python?
- r - 使用 R 中的表达式函数更改图例中的文本字体