Can I make column names all lower case upon import using pandas read_csv?

Viewed 1790

I am parsing a payload and want to import only those columns that I am including in my model. I can easily do this in pandas using:

df = pd.read_csv(StringIO(values), delimiter=',', usecols=LIST_COLS)

However, my fear is that sometimes the column names (often camel case) will not match the column names in the model. Thus, prior to training my model, I converted all column names to lower case. Now, when I import my data I want to import the column names as lower to ensure everything will match up when all portions of the payload are concatenated and a prediction needs to be generated.

I am assuming this is possible but I do not know how to do this.

Thank you in advance.

2 Answers

From the docs of pandas.read_csv

usecols : list-like or callable, optional

Modify your code as below using callable lambda function:

df = pd.read_csv(StringIO(values), delimiter=',', usecols=lambda x: x.lower() in LIST_COLS)

I found the answer here.

df = pd.read_csv(StringIO(values), delimiter=',', usecols=lambda col: col.lower() in LIST_COLS)

Then, I can convert them to lower using:

df.columns = df.columns.str.lower() # from Alexander's answer
Related