KEXIN WANG
KEXIN WANG

Reputation: 143

Case insensitive matching for pandas dataframe columns

I have a dataframe and a row select function.

import pandas as pd
df = pd.DataFrame({'A':[1,2,3],'B':[5,6,7]})
def select_f(row):
    return row['a']

The question is I don't want to change the columns name(keep uppercase) and make the following function run

for _, row in df.iterrows:
    if select_f(row) >2:
        print row['B']

Upvotes: 1

Views: 1959

Answers (1)

cs95
cs95

Reputation: 402533

You could use df.filter with a regex:

In [246]: df.filter(regex=re.compile('^a$', re.I))
Out[246]: 
   A
0  1
1  2
2  3

For your purpose, you'd use:

def select_f(row):
    return row.filter(regex=re.compile('^a$', re.I)).iloc[0]

Upvotes: 1

Related Questions