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

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)
Asked By: Stormy



You can try

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

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

Answered By: wwnde
Answered By: Li Yupeng
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.