python - 将 pandas 数据帧转换为 JSON,字符串分隔
问题描述
我有一个名为“df”的 pandas.dataframe,格式如下:
团队名字 | Positive_Sentiment | Negative_Sentiment |
---|---|---|
组1 | 乐于助人,大力支持 | 客户服务慢,界面薄弱,管理不善 |
我想将此数据框转换为具有以下格式的 JSON 文件:
[{
"Group Name": "group1",
"Postive Sentiment": [
"helpful",
"great support"
],
"Negative Sentiment": [
"slow customer service",
"weak interface",
"bad management"
]
}
]
到目前为止,我已经使用了这个:
import json
b = []
for i in range(len(df)):
x={}
x['Group Name']=df.iloc[i]['group_name']
x['Positive Sentiment']= [df.iloc[i]['Positive_Sentiment']]
x['Negative Sentiment']= [df.iloc[i]['Negative_Sentiment']]
b.append(x)
##Export
with open('AnalysisResults.json', 'w') as f:
json.dump(b, f, indent = 2)
这导致:
[{
"Group Name": "group1",
"Postive Sentiment": [
"helpful,
great support"
],
"Negative Sentiment": [
"slow customer service,
weak interface,
bad UX"
]
}
]
你可以看到它非常接近。关键的区别是每行的整个内容都用双引号引起来(例如,“helpful, great support”),而不是行中每个逗号分隔的字符串(例如,“helpful”、“great support”)。我想在每个字符串周围加上双引号。
解决方案
您可以应用split(",")
到您的列:
from io import StringIO
import pandas as pd
import json
inp = StringIO("""group_name Positive_Sentiment Negative_Sentiment
group1 helpful, great support slow customer service, weak interface, bad management
group2 great, good support interface meeeh, bad management""")
df = pd.read_csv(inp, sep="\s{2,}")
def split_and_strip(sentiment):
[x.strip() for x in sentiment.split(",")]
df["Positive_Sentiment"] = df["Positive_Sentiment"].apply(split_and_strip)
df["Negative_Sentiment"] = df["Negative_Sentiment"].apply(split_and_strip)
print(json.dumps(df.to_dict(orient="record"), indent=4))
# to save directly to a file:
with open("your_file.json", "w+") as f:
json.dump(df.to_dict(orient="record"), f, indent=4)
输出:
[
{
"group_name": "group1",
"Positive_Sentiment": [
"helpful",
"great support"
],
"Negative_Sentiment": [
"slow customer service",
"weak interface",
"bad management"
]
},
{
"group_name": "group2",
"Positive_Sentiment": [
"great",
"good support"
],
"Negative_Sentiment": [
"interface meeeh",
"bad management"
]
}
]
推荐阅读
- android - How do I use MVVM with bound service?
- javascript - ReactJS:如何在值中传递多个值?
- android - 什么是 kapt 异常?
- python - Python中基于括号的函数复合
- html - 试图居中- 这与clearfix有关吗?
- python-3.x - Python中的最大非连续子数组
- vue.js - Vue 2 的 __vue__ 的 Vue 3 等价物是什么?
- reactjs - 创建 React App 构建无法正常工作
- java - Java vs PHP - 方法参数中的引用
- wordpress - 在 allTribeEvents(事件日历)中显示 ACF 查询