小组回答&基于 python 中的问题的索引
我有一个用于分析目的的数据框,我需要创建一个字典列表:
目标输出
[
{ '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
Well yes, the obvious method is to use groupby but facing some errors
I tried printing before converting to list. And it seems fine actually,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要实际选择要在列表中显示其值的列(
“index”
):而不是否则
您将获得列的列表,如示例所示。这就是当您将 DataFrame 转换为列表时发生的情况,即
list(data)
给出的结果与list(data.columns)
相同。You need to actually select the column (
"index"
) whose values you want to appear in the list:instead of
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 aslist(data.columns)
.