groupby逗号分隔值在单个DataFrame列python / pandas中(groupby comma

系统教程 行业动态 更新时间:2024-06-14 17:02:18
groupby逗号分隔值在单个DataFrame列python / pandas中(groupby comma-separated values in single DataFrame column python/pandas)

作为一个例子,假设我有一个python pandas DataFrame,如下所示:

# PERSON THINGS 0 Joe Candy Corn, Popsicles 1 Jane Popsicles 2 John Candy Corn, Ice Packs 3 Lefty Ice Packs, Hot Dogs

我想使用熊猫groupby功能有以下输出:

THINGS COUNT Candy Corn 2 Popsicles 2 Ice Packs 2 Hot Dogs 1

我通常了解以下groupby命令:

df.groupby(['THINGS']).count()

但是输出不是单个项目,而是整个字符串。 我想我明白为什么会出现这种情况,但我不清楚如何最好地处理问题以获得所需的输出,而不是以下内容:

THINGS PERSON Candy Corn, Ice Packs 1 Candy Corn, Popsicles 1 Ice Packs, Hot Dogs 1 Popsicles 1

大熊猫是否有像SQL中的LIKE这样的函数,或者我在考虑在熊猫中如何做到这一点?

任何援助赞赏。

As an example, let's say I have a python pandas DataFrame that is the following:

# PERSON THINGS 0 Joe Candy Corn, Popsicles 1 Jane Popsicles 2 John Candy Corn, Ice Packs 3 Lefty Ice Packs, Hot Dogs

I would like to use the pandas groupby functionality to have the following output:

THINGS COUNT Candy Corn 2 Popsicles 2 Ice Packs 2 Hot Dogs 1

I generally understand the following groupby command:

df.groupby(['THINGS']).count()

But the output is not by individual item, but by the entire string. I think I understand why this is, but it's not clear to me how to best approach the problem to get the desired output instead of the following:

THINGS PERSON Candy Corn, Ice Packs 1 Candy Corn, Popsicles 1 Ice Packs, Hot Dogs 1 Popsicles 1

Does pandas have a function like the LIKE in SQL, or am I thinking about how to do this wrong in pandas?

Any assistance appreciated.

最满意答案

通过分词来创建一个系列,并使用value_counts

In [292]: pd.Series(df.THINGS.str.cat(sep=', ').split(', ')).value_counts() Out[292]: Popsicles 2 Ice Packs 2 Candy Corn 2 Hot Dogs 1 dtype: int64

Create a series by splitting words, and use value_counts

In [292]: pd.Series(df.THINGS.str.cat(sep=', ').split(', ')).value_counts() Out[292]: Popsicles 2 Ice Packs 2 Candy Corn 2 Hot Dogs 1 dtype: int64

更多推荐

本文发布于:2023-04-21 18:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9db46880747feac94a48fe362eaf6ddc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   DataFrame   groupby   comma   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!