'Select rows by partial string match in index
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?
Solution 1:[1]
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]]
Solution 2:[2]
An alternative (to Andy Hayden's answer) using filter
, see here:
>>> ds.filter(like='wiki', axis=0)
wikimedia 22
wikipedia 10
wikitravel 33
dtype: int64
Solution 3:[3]
From the original question:
"...index cries for 'contains' just like what the columns have".
I'm not sure when this was added (this is an old question), but you can now use contains
on index.str
assuming your index is a string type:
>>> import pandas as pd
>>>
>>> ds = pd.Series({'wikipedia':10,'wikimedia':22,'wikitravel':33,'google':40})
>>> ds[ds.index.str.contains("wiki")]
wikipedia 10
wikimedia 22
wikitravel 33
dtype: int64
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 | |
Solution 2 | s_pike |
Solution 3 | s_pike |