Select the dataframe row from rowname using case-case (e.g. `grep -i`)

I have a dataframe that looks like this:

In [1]: mydict = {"1421293_at Hdgfl1":[2.140412,1.143337,3.260313],
                  "1429877_at Lrriq3":[9.019368,0.874524,2.051820]}

In [3]: import pandas as pd

In [4]:  df = pd.DataFrame.from_dict(mydict, orient='index')

In [5]: df
Out[5]:
                          0         1         2
1421293_at Hdgfl1  2.140412  1.143337  3.260313
1429877_at Lrriq3  9.019368  0.874524  2.051820

      

What I want to do is select a string from the string name using a case insensitive query. For example, if the query is "hdgfl1", it should return:

                                         0                1               2
1421293_at Hdgfl1                 2.140412          1.143337          3.260313

      

"hdgfl1" is a case-insensitive register "1421293_at Hdgfl1". Mostly equivalent grep -i

.

How can I do this?

+3


source to share


3 answers


In [229]: df.filter(regex=r'(?i)hdgfl1', axis=0)
Out[229]: 
                          0         1         2
1421293_at Hdgfl1  2.140412  1.143337  3.260313

      



+3


source


You can do it like this:

query = 'hdgfl1'
mask = df.index.to_series().str.contains(query, case=False)
df[mask]

      

Another possibility:



mask = df.reset_index()['index'].str.contains(query, case=False)

      

but this is 2x slower.

+4


source


And using select ():

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re

mydict = {
"1421293_at Hdgfl1":[2.140412,1.143337,3.260313],
"1429877_at Lrriq3":[ 9.019368,0.874524,2.051820],
"1421293_at hDGFl1":[2.140412,1.143337,3.260313],
}

df = pd.DataFrame.from_dict(mydict, orient='index')

def create_match_func(a_str):
    def match_func(x):
        pattern = r".* {}".format(a_str)
        match_obj = re.search(pattern, x, flags=re.X|re.I)
        return match_obj

    return match_func

print df
print '-' * 20

target = "hdgfl1"
print df.select(create_match_func(target), axis=0)

--output:--
                          0         1         2
1421293_at Hdgfl1  2.140412  1.143337  3.260313
1429877_at Lrriq3  9.019368  0.874524  2.051820
1421293_at hDGFl1  2.140412  1.143337  3.260313
--------------------
                          0         1         2
1421293_at Hdgfl1  2.140412  1.143337  3.260313
1421293_at hDGFl1  2.140412  1.143337  3.260313

      

...

df.select(lambda x: x == 'A', axis=1)

      

select()

takes a function

that works with the label (s) along axis

, and the function should return a boolean

.

http://pandas.pydata.org/pandas-docs/stable/indexing.html#the-select-method

-1


source







All Articles