[英]Convert pandas data frame to JSON with strings separated
我有一個名為“df”的 pandas.dataframe,格式如下:
團隊名字 | Positive_Sentiment | 負面情緒 |
---|---|---|
第一組 | 樂於助人,大力支持 | 客戶服務緩慢,界面薄弱,管理不善 |
我想將此數據幀轉換為具有以下格式的 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"
]
}
]
你可以看到它非常接近。 關鍵的區別在於每行的整個內容周圍都用雙引號(例如,“有用,非常支持”)而不是行中的每個逗號分隔字符串(例如,“有用”,“非常支持”)。 我想在每個字符串周圍加上雙引號。
您可以將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,}")
df["Positive_Sentiment"] = df["Positive_Sentiment"].apply(lambda x : x.split(","))
df["Negative_Sentiment"] = df["Negative_Sentiment"].apply(lambda x : x.split(","))
print(json.dumps(df.to_dict(orient="record"), 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"
]
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.