首页 > 解决方案 > Flag if the last element in a group contain particular string in Pandas

问题描述

I have data:

Group   string
 A     Hello
 A     SearchListing
 A     GoSearch
 A     pen
 A     Hello
 A     Searches
 B     Real-Estate
 B     Access
 B     Denied
 B     Group
 B     Group
 C     Glance
 C     NoSearch
 C     Home
 C     FullSearch

and so on.

I want to find those group which has a string containing "search" as their last element and flag them. For example, the output i want is something like this:

  Group   Flag
   A       1
   B       0
   C       1

as both A and C had their last element containing string "search". I know of a method which can iterate through all the elements and if last element contains "search" it will flag it. But its a very heavy function using loops. Is there a more straight forward way for this?

标签: pythonpandasnumpygroup-bypandas-groupby

解决方案


使用str.contains

(df.groupby('Group')['string']
   .last()
   .str.contains('search', case=False)
   .astype(int)
   .rename('Flag')
   .reset_index())

  Group  Flag
0     A     1
1     B     0
2     C     1

与上面类似(复制或重置索引以避免SettingWithCopyWarning)。

u = df.drop_duplicates('Group', keep='last').reset_index(drop=True)
u['Flag'] = u.pop('string').str.contains('search', case=False).astype(int)
u

  Group  Flag
0     A     1
1     B     0
2     C     1

推荐阅读