【发布时间】:2021-06-26 00:05:17
【问题描述】:
我有下表:
| Pattern | tag | Responses |
|---|---|---|
| hi | greeting | Hey :-) |
| hey | greeting | Hello, thanks for visiting |
| how are you | greeting | Hi there, what can I do for you? |
| Is anyone there? | greeting | Hi there, how can I help? |
| Bye | goodbye | See you later, thanks for visiting |
| See you later | goodbye | Have a nice day |
| Goodbye | goodbye | Bye! Come back again soon |
我想通过“标签”对其进行分组并获得以下json格式:
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Hi",
"Hey",
"How are you",
"Is anyone there?"
],
"responses": [
"Hey :-)",
"Hello, thanks for visiting",
"Hi there, what can I do for you?",
"Hi there, how can I help?"
]
},
{
"tag": "goodbye",
"patterns": ["Bye", "See you later", "Goodbye"],
"responses": [
"See you later, thanks for visiting",
"Have a nice day",
"Bye! Come back again soon"
]
}
]
}
我尝试了很多东西,例如以下:
df.groupby('tag').apply(lambda x: list(x['Pattern']))
但这并不是我想要的……有人有什么建议吗?
【问题讨论】:
标签: python json dataframe dictionary