简体   繁体   English

有没有办法根据特定值过滤 dataframe,但也使用 pandas 保留唯一标识符的所有其他值?

[英]Is there a way to filter a dataframe based on a specific value but also keep all other values for the unique identifier using pandas?

What I mean is that let's say we have the following dataframe:我的意思是,假设我们有以下 dataframe:

UID  A  
1    Yes  
1    No  
2    No  
2    No  
3    Yes  
3    No  
4    Yes  
4    Yes  

I want to produce a dataframe where any UID that has a yes is included, even the other instances of the UID is a "No".我想生成一个 dataframe ,其中包含任何具有是的 UID,即使 UID 的其他实例也是“否”。

UID  A  
1    Yes  
1    No  
3    Yes  
3    No  
4    Yes  
4    Yes  

Is there a way to do this using Pandas or any other library in python?有没有办法使用 Pandas 或 python 中的任何其他库来做到这一点?

Try with isin试试isin

df = df.loc[df.UID.isin(df.loc[df.A=='Yes','UID'])]
df
Out[323]: 
   UID    A
0    1  Yes
1    1   No
4    3  Yes
5    3   No
6    4  Yes
7    4  Yes

I would use a groupby + filter operation:我会使用 groupby + filter 操作:

result = (
    df.groupby('UID')
      .filter(lambda g: g['A'].eq('Yes').any()
)

And that gives me:这给了我:

   UID    A
0    1  Yes
1    1   No
4    3  Yes
5    3   No
6    4  Yes
7    4  Yes

Try尝试

list = [x for x in df['UID'] if df['A'] == 'Yes']
dfnew = df.query('UID == list & A == ("Yes","No")')

Not elegant but worth a try不优雅但值得一试

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

相关问题 根据一列过滤熊猫数据框:保留所有行(如果值是该列) - Filter pandas dataframe based on a column: keep all rows if a value is that column 根据其他列中的值查询 pandas dataframe 值的更快方法 - Faster way to query pandas dataframe for a value based on values in other column 获取 Pandas dataframe 的样本,但保留所有唯一值 - Get sample of Pandas dataframe but keep all unique values 根据另一个数据框中特定值的所有实例更新熊猫数据框中的所有值 - Updated all values in a pandas dataframe based on all instances of a specific value in another dataframe PYTHON Pandas - 基于其他数据帧中的值对数据帧使用 Pandas 样式 - PYTHON Pandas - Using Pandas Styling for dataframe based on values in other dataframe 过滤分组的熊猫数据框,保留列中具有最小值的所有行 - Filter grouped pandas dataframe, keep all rows with minimum value in column Pandas Pivot 数据基于 Dataframe 中的所有唯一值 - Pandas Pivot the data based on all unique values in Dataframe 根据其他数据框列值过滤熊猫数据框 - Filter pandas Data Frame Based on other Dataframe Column Values 根据特定月份的值过滤熊猫数据框,并以另一列为条件 - Filter a pandas Dataframe based on specific month values and conditional on another column 根据 pandas 中每个类别的特定值过滤 dataframe - filter a dataframe based on a specific value for each category in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM