小组回答&基于 python 中的问题的索引

发布于 2025-01-12 04:14:54 字数 981 浏览 0 评论 0原文

我有一个用于分析目的的数据框,我需要创建一个字典列表:

目标输出

[
{ 'is my anti hiv test conclusive or--Bla bla': [0, 1, 2] }, 
{'I have some hip pain 9 weeks--bla bla': [3, 4, 5, 6]} 
]

这里列表是答案索引,而不是实际的答案

在此处输入图像描述

是的,显而易见的方法是使用 groupby 但面临一些错误

在此处输入图像描述

我在转换之前尝试打印列出。实际上看起来不错,

在此处输入图像描述

你们能帮我弄清楚它的正确语法,以便我可以达到我的目标输出。

数据集链接 如果有人需要共享笔记本链接,请在评论中告诉我。

I have a dataframe as such for analysis purpose, I need to create a list of dictionaries as:

TARGET OUTPUT

[
{ 'is my anti hiv test conclusive or--Bla bla': [0, 1, 2] }, 
{'I have some hip pain 9 weeks--bla bla': [3, 4, 5, 6]} 
]

Here the list is indices of answers and not the actual answers

enter image description here

Well yes, the obvious method is to use groupby but facing some errors

enter image description here

I tried printing before converting to list. And it seems fine actually,

enter image description here

Can y'all please help me figure out it's correct syntax so I could to my targeted output.

Dataset link
If somebody needs the shared notebook link, let me know in the comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2025-01-19 04:14:54

您需要实际选择要在列表中显示其值的列(“index”):

df_ans = data.groupby(["question_text"])["index"].apply(list).to_dict()

而不是否则

df_ans = data.groupby(["question_text"]).apply(list).to_dict()

您将获得列的列表,如示例所示。这就是当您将 DataFrame 转换为列表时发生的情况,即 list(data) 给出的结果与 list(data.columns) 相同。

You need to actually select the column ("index") whose values you want to appear in the list:

df_ans = data.groupby(["question_text"])["index"].apply(list).to_dict()

instead of

df_ans = data.groupby(["question_text"]).apply(list).to_dict()

Otherwise you get a list of the columns, as in your example. That's what happens when you convert a DataFrame to a list, i.e. list(data) gives you the same as list(data.columns) .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文