Having a series like this:
ds = Series({'wikipedia':10,'wikimedia':22,'wikitravel':33,'google':40})
google 40
wikimedia 22
wikipedia 10
wikitravel 33
dtype: int64
I would like to select the rows where 'wiki' is a part of the index label (a partial string label).
For the moment I tried
ds[ds.index.map(lambda x: 'wiki' in x)]
wikimedia 22
wikipedia 10
wikitravel 33
Name: site, dtype: int64
and it does the job, but somehow the index cries for 'contains' just like what the columns have...
Any better way to do that?
To get the position of the first partial match (i.e. the cell that contains text you are looking for) you can use the MATCH function with wildcards. The MATCH function returns the position or "index" of the first match based on a lookup value in a range.
To do a Partial String Matching in R, use the charmatch() function. The charmatch() function accepts three arguments and returns the integer vector of the same length as input.
If you just want to find which name is partial match the given name, you also can use this formula =INDEX($E$2:$E$14,MATCH($K$1&"*",E2:E14,0)). (E2:E14 is the column list you want to lookup from, k1 is the given name, you can change as you need.)
Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.
A somewhat cheeky way could be to use loc
:
In [11]: ds.loc['wiki': 'wikj']
Out[11]:
wikimedia 22
wikipedia 10
wikitravel 33
dtype: int64
This is essentially equivalent to ds[ds.index.map(lambda s: s.startswith('wiki'))]
.
To do contains, as @DSM suggests, it's probably nicer to write as:
ds[['wiki' in s for s in ds.index]]
Another solution using filter
, see here:
>>> ds.filter(like='wiki', axis=0)
wikimedia 22
wikipedia 10
wikitravel 33
dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With