[英]How to keep multiple rows if they have the same value within a column AND at least one of them contains a string in another column
I have searched multiple threads and cannot seem to figure this out.我搜索了多个线程,似乎无法弄清楚这一点。 Any help would be appreciated.
任何帮助,将不胜感激。 Suppose I have the following data set (I have simplified it for the sake of this question)
假设我有以下数据集(为了这个问题,我已经简化了它)
I want to group together all rows that contain the same value in COL1 then search in COL2 for the string "red" for those specific rows.我想将所有在 COL1 中包含相同值的行组合在一起,然后在 COL2 中搜索那些特定行的字符串“red”。 If at least one of the rows in that group contains "red", then I want to keep all of those rows.
如果该组中至少有一行包含“红色”,那么我想保留所有这些行。 Thus, for this dataset, the output should look like this:
因此,对于这个数据集,output 应该如下所示:
Any help would be greatly appreciated.任何帮助将不胜感激。 I am working in python.
我在 python 工作。 Thank you!
谢谢!
df[df['col1'].isin(df[df['col2'] == 'red']['col1'])]
col1 col2
0 1 red
1 1 yellow
2 1 green
7 3 red
8 3 pink
9 3 green
Do you mean 'red' has value 1 and 3 in Col1, therefore you would like to keep all rows with value 1 and 3 in Col1?您的意思是“红色”在 Col1 中的值为 1 和 3,因此您希望在 Col1 中保留所有值为 1 和 3 的行吗? You can try this:
你可以试试这个:
df[df['Col1'].isin(df['Col1'][df['Col2']=='red'])]
To explain, I'm using a filter to extract the relevant rows:为了解释,我使用过滤器来提取相关行:
filter = df['Col1'][df['Col2']=='red']
df1 = df[df['Col1'].isin(filter)]
print(df1)
Output Output
Col1 Col2
0 1 red
1 1 yellow
2 1 green
6 3 red
7 3 pink
8 3 green
Using Groupby and checking if any rows in COL2 in group have color red使用 Groupby 并检查组中 COL2 中的任何行是否为红色
df[df.groupby("COL1").COL2.transform(lambda x: x.eq("red").any())]
Output Output
COL1 COL2
0 1 red
1 1 yellow
2 1 green
7 3 red
8 3 pink
9 3 green
Explanation解释
mask = df.groupby("COL1").COL2.transform(lambda x: x.eq("red").any())
mask is True if any items in group in COL2 have color red如果 COL2 中组中的任何项目具有红色,则掩码为 True
0 True
1 True
2 True
3 False
4 False
5 False
6 False
7 True
8 True
9 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.