Pandas: Refer to column name, case insensitive

P A N picture P A N · Apr 1, 2016 · Viewed 16.8k times · Source

Using Pandas DataFrame, let's say I have a bunch of columns in a csv file, and I want to be able to access any one of them via case insensitive name.

import pandas as pd

df = pd.read_csv(path_to_csv, delimiter=",")

df2 = df["Size"]

The actual column name is "Size". What can I do so that df2 = df["sIZE"] can also be accepted?

Answer

EdChum picture EdChum · Apr 1, 2016

you can just call str.lower on the columns:

In [12]:
df = pd.DataFrame(columns=['Size','COLOUR','caTegory'])
df.columns

Out[12]:
Index(['Size', 'COLOUR', 'caTegory'], dtype='object')

In [14]:
df.columns = df.columns.str.lower()
df.columns

Out[14]:
Index(['size', 'colour', 'category'], dtype='object')