Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Refer to column name, case insensitive

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?

like image 885
P A N Avatar asked Apr 01 '16 17:04

P A N


People also ask

How do you make column names lowercase in Pandas?

We can convert the names into lower case using Pandas' str. lower() function. We first take the column names and convert it to lower case. And then rename the Pandas columns using the lowercase names.

How do I ignore case in STR contain?

str. contains has a case parameter that is True by default. Set it to False to do a case insensitive match.

How do you capitalize column names in Pandas?

Convert Column Names to Uppercase using str. where, df is the input dataframe and columns is the attribute to get the column labels as an Index Object. Then using the StringMethods. upper() we converted all labels to uppercase. It converted all the column labels to uppercase.

Is merge in Pandas case sensitive?

By default Pandas merge method is case-sensitive. There should be a way to merge 2 dataframes without considering upper/ lower case just like SQL joins.

How to convert pandas column names to lowercase?

How To Convert Pandas Column Names to lowercase? We can convert the names into lower case using Pandas’ str.lower () function. We first take the column names and convert it to lower case. And then rename the Pandas columns using the lowercase names. Now our dataframe’s names are all in lower case.

How to select rows&columns by name or index in pandas?

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc. Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each.

What is indexing in pandas?

Last Updated : 10 Jul, 2020 Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each. Indexing is also known as Subset selection.

How to strip leading and trailing spaces from column names in pandas?

In addition to upper cases, sometimes column names can have both leading and trailing empty spaces. Let us create a toy dataframe with column names having trailing spaces. By inspecting column names we can see the spaces. We can use str.strip () function Pandas to strip the leading and trailing white spaces.


3 Answers

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')
like image 175
EdChum Avatar answered Oct 16 '22 16:10

EdChum


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.

like image 33
Daniel Severo Avatar answered Oct 16 '22 17:10

Daniel Severo


Why not normalize the column names in df?

df.columns = [c.lower() for c in df.columns]
like image 10
BlindDriver Avatar answered Oct 16 '22 16:10

BlindDriver