I want to change the headers of my dataframe based on a dictionary, but I need to reference the columns as lower case.
dict = {'january': 'Jan', 'february': 'Feb', etc.}
df.columns = ['JANUARY','FEBRUARY', etc.]
I know I can do something like this:
df = df.rename(columns=dict)
But this wouldn't work because the columns don't equal the keys. Is there a way to map the dict values to the headers without making a new dict or lower casing all the column headers? Can I map by df.columns.str.lower()
somehow?
CodePudding user response:
You could use str.lower
map
on df.columns
to modify it:
dct = {'january':'Jan','february':'Feb', 'march':'Mar', 'april':'Apr'}
df.columns = df.columns.str.lower().map(dct)
For the following DataFrame:
JANUARY FEBRUARY MARCH APRIL
0 0 0 1 0
1 0 1 1 1
the above code produces:
Jan Feb Mar Apr
0 0 0 1 0
1 0 1 1 1
CodePudding user response:
If I am not wrong, you need to change your header to something like:
jan feb mar apr may jun jul aug sep oct nov dec
I mean, not only the value of the dictionary but also all in lowercase.
So, the following approach will help you to get your answer:
import pandas as pd
dic = {'january': 'Jan', 'february': 'Feb', 'march': 'Mar', 'april': 'Apr', 'may': 'May', 'june': 'Jun', 'july': 'Jul',
'august': 'Aug', 'september': 'Sep', 'october': 'Oct', 'november': 'Nov', 'december': 'Dec'}
data = {'JANUARY': [2, 16, 19, 15],
'FEBRUARY': [20, 8, 4, 5],
'MARCH': [1, 2, 4, 11],
'APRIL': [14, 4, 20, 13],
'MAY': [6, 4, 13, 1],
'JUNE': [6, 2, 19, 14],
'JULY': [17, 8, 13, 19],
'AUGUST': [0, 8, 9, 15],
'SEPTEMBER': [6, 3, 3, 18],
'OCTOBER': [1, 8, 6, 15],
'NOVEMBER': [18, 20, 8, 3],
'DECEMBER': [18, 17, 7, 16]}
df = pd.DataFrame.from_dict(data)
print(df)
Result:
JANUARY FEBRUARY MARCH APRIL ... SEPTEMBER OCTOBER NOVEMBER DECEMBER
0 2 20 1 14 ... 6 1 18 18
1 16 8 2 4 ... 3 8 20 17
2 19 4 4 20 ... 3 6 8 7
3 15 5 11 13 ... 18 15 3 16
df.columns = df.columns.str.lower().map(dict((k, v.lower()) for k, v in dic.items()))
print(df)
result:
jan feb mar apr may jun jul aug sep oct nov dec
0 2 20 1 14 6 6 17 0 6 1 18 18
1 16 8 2 4 4 2 8 8 3 8 20 17
2 19 4 4 20 13 19 13 9 3 6 8 7
3 15 5 11 13 1 14 19 15 18 15 3 16