Reputation: 5922
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?
Upvotes: 30
Views: 36604
Reputation: 394051
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')
Upvotes: 30
Reputation: 1848
Have you tried changing the column names using df.columns to all lower or upper case? You can do that using
df.columns = map(str.lower, df.columns)
Maybe that can solve your issue.
Upvotes: 18
Reputation: 681
Why not normalize the column names in df
?
df.columns = [c.lower() for c in df.columns]
Upvotes: 13