Python 需要确定组在数据帧中是否只包含一个类别

Python 需要确定组在数据帧中是否只包含一个类别,python,pandas,Python,Pandas,我目前有一个id和一个名为“childOrParent”的列的以下数据帧。 一个群体不能有没有父母的孩子 +----+---------------+ | id | childOrParent | +----+---------------+ | 1 | Parent | | 1 | child | | 2 | Parent | | 3 | child | | 3 | child | | 3 | Parent

我目前有一个id和一个名为“childOrParent”的列的以下数据帧。 一个群体不能有没有父母的孩子

+----+---------------+
| id | childOrParent |
+----+---------------+
|  1 | Parent        |
|  1 | child         |
|  2 | Parent        |
|  3 | child         |
|  3 | child         |
|  3 | Parent        |
+----+---------------+
如何检查数据帧是否有效?如果有一个id组只有孩子,那么我需要知道id

ex)以下数据帧将无效,我需要知道它是id:3

+----+---------------+
| id | childOrParent |
+----+---------------+
|  1 | Parent        |
|  1 | child         |
|  2 | Parent        |
|  3 | child         |
|  3 | child         |
|  3 | child         |
+----+---------------+

我尝试只获取组中的子数据帧或父数据帧的计数,然后合并两个数据帧,但这似乎不正确

使用
groupby
filter
+
all

df.groupby('id').filter(lambda x : (x['childOrParent']=='child').all())
Out[383]: 
   id childOrParent
3   3         child
4   3         child
5   3         child
df.groupby('id').filter(lambda x : (x['childOrParent']=='child').all()).id.unique()
Out[384]: array([3], dtype=int64)

使用
groupby
filter
+
all

df.groupby('id').filter(lambda x : (x['childOrParent']=='child').all())
Out[383]: 
   id childOrParent
3   3         child
4   3         child
5   3         child
df.groupby('id').filter(lambda x : (x['childOrParent']=='child').all()).id.unique()
Out[384]: array([3], dtype=int64)