'Filtering dataframe based on two column string match count
I want to filter data rows based on string from data['Age'] and count atleast two occurence of that pattern "o;", "i;", "twenty;", "a;" in data['Name'].
data = pd.DataFrame({'Name':['To;om', 'ni;cki;', 'krish', 'jack'],'Age':['o', 'i', 'twenty', 'a']})
data
Name Age
0 To;om o
1 ni;cki; i
2 krish twenty
3 jack a
Output should look like this:
Name Age count
0 ni;cki; i 2
Solution 1:[1]
List comprehension to find the counts and then filtering with it:
df["count"] = [name.count(age) for name, age in zip(df.Name, df.Age)]
out = df[df["count"] >= 2].copy()
to get
>>> out
Name Age count
1 ni;cki; i 2
The copy at the end is to avoid the SettingWithCopyWarning in possible future manipulations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mustafa Ayd?n |
