Reputation: 21552
I have a Series of keywords extracted from a bigger DataFrame and a DataFrame with, among others, a column of strings. I would like to mask the DataFrame finding which strings contains at least one keyword. The "Keywords" Series is as follows (sorry for the weird words):
Skilful
Wilful
Somewhere
Thing
Strange
The DataFrame looks as follows:
User_ID;Tweet
01;hi all
02;see you somewhere
03;So weird
04;hi all :-)
05;next big thing
06;how can i say no?
07;so strange
08;not at all
So far I used a str.contains() function from pandas like:
mask = df['Tweet'].str.contains(str(Keywords['Keyword'][4]), case=False)
which works well finding the "Strange" string in the DataFrame and returns:
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 False
Name: Tweet, dtype: bool
What I would like to do is to mask the whole DataFrame with the all Keywords array, so I can have something like this:
0 False
1 True
2 False
3 False
4 True
5 False
6 True
7 False
Name: Tweet, dtype: bool
Is it possible without looping through the array? In my real case I have to search through millions of strings, so I'm looking for a fast method.
Thanks for your kind help.
Upvotes: 0
Views: 2041
Reputation: 20553
Another way to achieve this is to use pd.Series.isin() with map and apply, with your sample it will be like:
df # DataFrame
User_ID Tweet
0 1 hi all
1 2 see you somewhere
2 3 So weird
3 4 hi all :-)
4 5 next big thing
5 6 how can i say no?
6 7 so strange
7 8 not at all
w # Series
0 Skilful
1 Wilful
2 Somewhere
3 Thing
4 Strange
dtype: object
# list
masked = map(lambda x: any(w.apply(str.lower).isin(x)), \
df['Tweet'].apply(str.lower).apply(str.split))
df['Tweet_masked'] = masked
Results:
df
Out[13]:
User_ID Tweet Tweet_masked
0 1 hi all False
1 2 see you somewhere True
2 3 So weird False
3 4 hi all :-) False
4 5 next big thing True
5 6 how can i say no? False
6 7 so strange True
7 8 not at all False
As a side note, isin only works if the whole string matches the values, in case you are only interested in str.contains
, here's the variant:
masked = map(lambda x: any(_ in x for _ in w.apply(str.lower)), \
df['Tweet'].apply(str.lower))
Updated: as @Alex pointed out, it could be even more efficient to combine both map and regexp, in fact I don't quite like map + lambda neither, here we go:
import re
r = re.compile(r'.*({}).*'.format('|'.join(w.values)), re.IGNORECASE)
masked = map(bool, map(r.match, df['Tweet']))
Upvotes: 1
Reputation: 19104
import re
df['Tweet'].str.match('.*({0}).*'.format('|'.join(phrases)))
Where phrases
is an iterable of phrases whose existence you are conditioning on.
Upvotes: 2
Reputation: 14169
A simple apply
can solve this. If you can weather a few seconds of processing, I think this is the simplest method available to you without venturing outside pandas
.
import pandas as pd
df = pd.read_csv("dict.csv", delimiter=";")
ref = pd.read_csv("ref.csv")
kw = set([k.lower() for k in ref["Keywords"]])
print kw
boom = lambda x:True if any(w in kw for w in x.split()) else False
df["Tweet"] = df["Tweet"].apply(boom)
print df
I tested it against exactly 10,165,760 rows of made-up data and it completed in 18.9s. If that is not fast enough, then a better method is needed.
set(['somewhere', 'thing', 'strange', 'skilful', 'wilful'])
User_ID Tweet
0 1 False
1 2 True
2 3 False
3 4 False
4 5 True
5 6 False
6 7 True
7 8 False
8 1 False
9 2 True
10 3 False
11 4 False
12 5 True
13 6 False
14 7 True
15 8 False
16 1 False
17 2 True
18 3 False
19 4 False
20 5 True
21 6 False
22 7 True
23 8 False
24 1 False
25 2 True
26 3 False
27 4 False
28 5 True
29 6 False
... ... ...
10165730 3 False
10165731 4 False
10165732 5 True
10165733 6 False
10165734 7 True
10165735 8 False
10165736 1 False
10165737 2 True
10165738 3 False
10165739 4 False
10165740 5 True
10165741 6 False
10165742 7 True
10165743 8 False
10165744 1 False
10165745 2 True
10165746 3 False
10165747 4 False
10165748 5 True
10165749 6 False
10165750 7 True
10165751 8 False
10165752 1 False
10165753 2 True
10165754 3 False
10165755 4 False
10165756 5 True
10165757 6 False
10165758 7 True
10165759 8 False
[10165760 rows x 2 columns]
[Finished in 18.9s]
Hope this helps.
Upvotes: 0