Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select rows by a pattern on the index column

Please find below my input/output (desired):

Input :

        OBID    NAME   VALUE
0  ID-110503  Name 1    39.0
1  ID-110504  Name 2   243.5
2  ID-225930  Name 3  3212.0
3  ID-339630  Name 4   350.0
4  ID-117742  Name 5   785.0

After setting the column OBID as the index, I need to select all the rows (by using df.loc) that have an index matching the pattern ID-11xxxx.

Output (desired) :

             NAME  VALUE
OBID                    
ID-110503  Name 1   39.0
ID-110504  Name 2  243.5
ID-117742  Name 5  785.0

I’m sorry if this is a stupid and/or a duplicated question but here is (if needed) a reproducible example.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
from io import StringIO

s = """OBID NAME    VALUE
ID-110503   Name 1  39
ID-110504   Name 2  243.5
ID-225930   Name 3  3212
ID-339630   Name 4  350
ID-117742   Name 5  785
"""

df = pd.read_csv(StringIO(s), sep='\t')
df.set_index('OBID', inplace=True)

>Solution :

Many ways.

Can use query and find string that begins with the characters you want leveraging str.contains

df.query("OBID.str.contains('^ID-11')", engine='python')

Using loc

df.loc[df.index.str.contains('^ID-11'),:]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading