Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select rows by partial string match in index

Tags:

python

pandas

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?

like image 808
ronszon Avatar asked May 17 '13 20:05

ronszon


People also ask

How do I match a partial string in Excel?

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.

How do you match a partial string in R?

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.

How do I find a partial match in Excel?

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.)

How do you match a partial string in Python?

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.


2 Answers

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]]
like image 193
Andy Hayden Avatar answered Sep 23 '22 15:09

Andy Hayden


Another solution using filter, see here:

>>> ds.filter(like='wiki', axis=0)
wikimedia     22
wikipedia     10
wikitravel    33
dtype: int64
like image 20
Chris Avatar answered Sep 21 '22 15:09

Chris