gpt4 book ai didi

python-3.x - 如何查找另一列的不同行中具有多个值的列值的总长度

转载 作者:行者123 更新时间:2023-12-02 19:09:41 24 4
gpt4 key购买 nike

有没有办法找到同时有Apple和Strawberry的ID,然后求总长度?以及只有 Apple 的 ID,以及只有 Strawberry 的 IDS?

df:

        ID           Fruit
0 ABC Apple <-ABC has Apple and Strawberry
1 ABC Strawberry <-ABC has Apple and Strawberry
2 EFG Apple <-EFG has Apple only
3 XYZ Apple <-XYZ has Apple and Strawberry
4 XYZ Strawberry <-XYZ has Apple and Strawberry
5 CDF Strawberry <-CDF has Strawberry
6 AAA Apple <-AAA has Apple only

期望的输出:

Length of IDs that has Apple and Strawberry: 2
Length of IDs that has Apple only: 2
Length of IDs that has Strawberry: 1

谢谢!

最佳答案

如果 Fruit 列中的所有值始终仅为 AppleStrawberry,您可以比较每组的集合,然后计算 ID 通过 True 值的 sum:

v = ['Apple','Strawberry']
out = df.groupby('ID')['Fruit'].apply(lambda x: set(x) == set(v)).sum()
print (out)
2

编辑:如果有很多值:

s = df.groupby('ID')['Fruit'].agg(frozenset).value_counts()
print (s)
{Apple} 2
{Strawberry, Apple} 2
{Strawberry} 1
Name: Fruit, dtype: int64

关于python-3.x - 如何查找另一列的不同行中具有多个值的列值的总长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64495124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com