Pandas分组和筛选

2024-05-13 10:40:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的.csv

  Name  Location    Product Type    number
Greg    1       Fruit   grape   1
Greg    1       Fruit   apple   2
Greg    1       Bakery  bread   5
Greg    1       Bakery  roll    8
Greg    2       Fruit   grape   7
Greg    2       Fruit   apple   1
Greg    3       Fruit   grape   2
Greg    4       Bakery  roll    3
Greg    4       Bakery  bread   4
Sam 5       Fruit   apple   7
Sam 5       Fruit   grape   9
Sam 5       Fruit   apple   10
Sam 6       Bakery  roll    11
Sam 6       Bakery  bread   12
Sam 7       Fruit   orange  13
Sam 7       Bakery  roll    14
Tim 8       Fruit   bread   16
Zack    9       Bakery  roll    17
Zack    10      Fruit   apple   19
Zack    10      Fruit   grape   20

我想按名称将其分为熊猫和大熊猫组,在多个地点有两种以上的产品。我仍然希望保留产品的“编号”

例如,位置1的Greg有两个产品

name    location    product     type
Greg    1       Fruit, bakery   grape,apple,bread,roll

我正在与groupby进行斗争,并最终将其恢复到一个我可以使用的数据框架中


Tags: csvnameapple产品samtypelocationproduct
2条回答

如果执行df.groupby([col_names]),列名称将成为索引

为了将索引转换回列,需要使用DataFrame.reset_index()方法

希望有帮助

IIUC将transformnunique一起使用

df1=df[df.groupby(['Name','Location']).Product.transform('nunique')>1]
    Name  Location Product    Type  number
0   Greg         1   Fruit   grape       1
1   Greg         1   Fruit   apple       2
2   Greg         1  Bakery   bread       5
3   Greg         1  Bakery    roll       8
14   Sam         7   Fruit  orange      13
15   Sam         7  Bakery    roll      14

相关问题 更多 >