Home > Back-end >  pandas multiindex columns rename
pandas multiindex columns rename

Time:11-21

hi I would like to rename the columns of my df. it has a multiindex columns and I would like to change the second level of it ie I have :

('GDP US Chained 2012 Dollars SAAR', 'GDP CHWG Index') ('GDP US Personal Consumption Chained 2012 Dollars SAAR', 'GDPCTOT Index') ('US Gross Private Domestic Investment Total Chained 2012 SAAR', 'GPDITOTC Index')
1969-12-31 00:00:00 4947.1 3052.12 593.659
1970-03-31 00:00:00 4939.76 3071.06 575.953
1970-06-30 00:00:00 4946.77 3084.97 577.205
1970-09-30 00:00:00 4992.36 3112.01 586.598
1970-12-31 00:00:00 4938.86 3103.57 555.454

I would like to change the second row column and replace The "index" with "" and delete the ' '. I tried :

df.columns.get_level_values(1).str.lower().str.replace('index', '', regex=True).str.strip()

it works but I can not put it in the column name

CodePudding user response:

df.columns = pd.MultiIndex.from_tuples([ (col0, col1.lower().replace('index', ''), *more_cols) for col0, col1, *more_cols in df.columns], names=df.columns.names)

shoud do

CodePudding user response:

Use rename with lambda function and parameter level=1:

L = [('GDP US Chained 2012 Dollars SAAR', 'GDP CHWG Index'),
    ('GDP US Personal Consumption Chained 2012 Dollars SAAR', 'GDPCTOT Index'),
    ('US Gross Private Domestic Investment Total Chained 2012 SAAR', 'GPDITOTC Index')]

c = pd.MultiIndex.from_tuples(L)
df = pd.DataFrame(columns=c, index=[0])

df = df.rename(columns=lambda x: x.lower().replace('index','').strip(), level=1)
print (df)
  GDP US Chained 2012 Dollars SAAR  \
                          gdp chwg   
0                              NaN   

  GDP US Personal Consumption Chained 2012 Dollars SAAR  \
                                                gdpctot   
0                                                NaN      

  US Gross Private Domestic Investment Total Chained 2012 SAAR  
                                                      gpditotc  
0                                                NaN            
  • Related