Pandas sort via multiple columns and key

So I have a pandas dataframe and I’m happy sorting it by several different columns. However, I’d like the first column (usernames) to be sorted by a lowercase value in addition to the column sorting and I can’t find anything that includes both of these.

This is what I have so far:

column sorting: df = df.sort_values(["Username", "Date", "Value"])

sorting without discrimination of upper/lowercase: key = lambda col: col.str.lower()

I just can’t combine them. Thanks in advance!

Answer

How about lower-casing the Username column and saving it as another column to use for the sort?

df['_username_lowercase'] = df['Username'].str.lower()
df = df.sort_values(["_username_lowercase", "Date", "Value"])

You can drop it after you’re done with the sort

df = df.drop('_username_lowercase', axis=1)