Pandas: see case-insensitive column name

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

 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?

+5
source share
3 answers

you can just call str.lower on 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') 
+3
source

Why not normalize column names in df ?

 df.columns = [c.lower() for c in df.columns] 
+4
source

Have you tried changing column names with df.columns to all lower or upper case? You can do it with

df.columns = map(str.lower, df.columns)

Perhaps this may solve your problem.

+3
source

Source: https://habr.com/ru/post/1246257/


All Articles