Home > Software engineering >  select rows by a pattern on the index column
select rows by a pattern on the index column

Time:09-18

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.

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)

CodePudding user response:

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'),:]

CodePudding user response:

You can try

df.iloc[:,num of rows].filter(regex= 'ID-11$',axis=0)

CodePudding user response:

df[df.index.str.startswith("ID-11")]
  • Related