从rowname中选择使用不区分大小写的数据帧行(如`grep -i`)

时间:2015-01-13 02:26:07

标签: python pandas select case-insensitive

我有一个如下所示的数据框:

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

我想要做的是使用不区分大小写的查询从行名称中选择行。 例如,给定查询“hdgfl1”它应返回:

                                         0                1               2
1421293_at Hdgfl1                 2.140412          1.143337          3.260313

“hdgfl1”是“1421293_at Hdgfl1”的不区分大小写的查询。基本上相当于grep -i

这样做的方法是什么?

3 个答案:

答案 0 :(得分:5)

你可以这样做:

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

另一种可能性是:

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

但速度慢了2倍。

答案 1 :(得分:4)

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

答案 2 :(得分:-1)

使用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()需要functionaxis上的标签进行操作 函数应返回a boolean

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