作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法找到同时有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
列中的所有值始终仅为 Apple
或 Strawberry
,您可以比较每组的集合,然后计算 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/
在我的 android 应用程序中,我想在同一行中实现 2 个文本字段。如果文本太长可以转到下一行。所以我使用了更精简的水平布局并添加了 2 个文本字段。 [例如:TextView1、TextV
由于node使用了javascript,它可以充当webrtc peer吗? 然后我可以将它编码为 VP8 流并广播给所有其他对等方吗? 最佳答案 WebRTC 是一个浏览器 API 而不是 Java
我是一名优秀的程序员,十分优秀!