包含列表的Pandas列上的分组操作 [英] Group operations on Pandas column containing lists

查看:111
本文介绍了包含列表的Pandas列上的分组操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame,其中包含一列props,其中包含字符串列表.

I have a DataFrame containing a column, props, which contains lists of strings.

理想情况下,我想按此列分组,但是我可以预料到的话会出错:

Ideally, I'd like to group by this column, but I predictably get an error when I do:

TypeError: unhashable type: 'list'

是否有一种明智的方法来重新排列我的DataFrame,以便我可以使用这些值?

Is there a sensible way to re-arrange my DataFrame so I can work with these values?

推荐答案

您可以将字符串列表转换为字符串元组.元组是不可哈希的,因为它们是不可变量的.当然,这是假设您在创建后不需要将这些列表添加到列表中或从列表中删除.

You can convert the lists of strings into tuples of strings. Tuples are hashable, as they are unmutable. This is of course assuming that you don't need to be adding to or removing from those lists after creation.

这篇关于包含列表的Pandas列上的分组操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆