简体   繁体   English

在另一列中查找具有相同值的行 - Python

[英]Find rows that have same values in another column - Python

I have a basic Python questions. 我有一个基本的Python问题。

I have a pandas dataframe like this: 我有一个像这样的pandas数据帧:

ID | Name | User_id
---+------+--------
 1   John     10
 2   Tom      11  
 3   Sam      12
 4   Ben      13
 5   Jen      10
 6   Tim      11
 7   Sean     14
 8   Ana      15
 9   Sam      12
 10  Ben      13

I want to get the names and user ids that share the same value for User_id, without returning names that appear twice. 我想获取与User_id共享相同值的名称和用户ID,而不返回出现两次的名称。 So I would like the output to look something like this: 所以我希望输出看起来像这样:

John Jen 10
Tom Tim 11

IIUC you could do it this way, groupby on 'User_id' and then filter the groupby: IIUC你可以这样来做, groupby上“USER_ID”,然后过滤GROUPBY:

In [54]:
group = df.groupby('User_id')['Name'].unique()

In [55]:
group[group.apply(lambda x: len(x)>1)]

Out[55]:
User_id
10    [John, Jen]
11     [Tom, Tim]
Name: Name, dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找与另一个数据帧中的列具有相同非唯一列值的数据帧行 - Find rows of a dataframe that have same non-unique column values as a column in another dataframe 如何按列名过滤值,然后将具有相同值的行提取到另一个CSV文件? Python /熊猫 - How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column Python:如何查找一列中的哪些值在另一特定列(数据框)中具有NaN值 - Python: How to find which values in a column have NaN values in another specific column (dataframes) 如果行在特定列中具有相同的值,则对特定行的值求和 - Sum the values of specific rows if the rows have same values in specific column dataframe 分别过滤掉一列相同但另一列有多个值的行 - Filter out rows that are the same in one column but have multiple values in another columns respectively in dataframe 从 Pandas Dataframe 中选择一列中具有相同值而另一列仅缺失的行 - Selecting rows from Pandas Dataframe with same values in one column that have only missing in another 在一个列中查找与另一列中的某些值有关联的值 - Find values in one column that have association with certain values in another column 如何在 python 的数据集中的任何列中找出一个或多个值为零的行 - how to find out rows that have one or more values as zero in any column of data set in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM