使用pandas dataFrame中的Groupby日期创建列表列表

发布于 2025-02-07 20:49:16 字数 323 浏览 2 评论 0原文

df =

        Date     Slot
0   2022-02-23    34
1   2022-02-23    35
2   2022-02-24     0
3   2022-02-24     1
4   2022-02-25     0
5   2022-02-25     1

这是我的df,我想要一个具有相同相应的“日期”值的所有“插槽”值的列表,即我的输出应该看起来像[[34,35],[0,1],[0,1],[0,[0, 1]]

ps:日期列中的值未经事先知道,因此在这里没有进行硬编码,是否有一般性执行此任务的概括。

df =

        Date     Slot
0   2022-02-23    34
1   2022-02-23    35
2   2022-02-24     0
3   2022-02-24     1
4   2022-02-25     0
5   2022-02-25     1

This is my df and I want a list of lists for all the 'Slot' values having the same corresponding 'Date' value i.e my output should look like [[34,35] , [0,1] , [0,1]]

PS: The values in the date column are not known beforehand so hardcoding is not working out here, is there a generalized to do this task.

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

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

发布评论

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

评论(1

初见你 2025-02-14 20:49:16

您可以尝试

out = df.groupby('Date')['Slot'].agg(list).tolist()
#
out = df.groupby('Date')['Slot'].apply(list).tolist()
print(out)

[[34, 35], [0, 1], [0, 1]]

You can try

out = df.groupby('Date')['Slot'].agg(list).tolist()
#
out = df.groupby('Date')['Slot'].apply(list).tolist()
print(out)

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