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?
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.
str. contains has a case parameter that is True by default. Set it to False to do a case insensitive match.
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.
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? 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.
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.
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.
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.
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')
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.
Why not normalize the column names in df
?
df.columns = [c.lower() for c in df.columns]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With