python - renaming column labels in multiindex df (built from excel) -


i'm reading excel file , manipulating content pandas df.

heres excel content looks (fig 1). , here's want df (fig 2) 1 exception - want rename column labels @ index level 0, "a4+" "all ind" , "a18-49" "comm demo".

the problem have when rename column labels come out inverted, not sure why?

here's code use..

    full_path_to_excel_file = path_runs+excel_file_name+'.xls'      df = pd.read_excel(full_path_to_excel_file, 0, header=none, index_col=0)     df = df.iloc[11:,:]     df = df.fillna(method='ffill', axis=1)      df.columns=pd.multiindex.from_arrays(df[:2].values)       #just name of index     df.index.name=none      #remove 3 rows used column names     df = df[pd.notnull(df.index)]      df = df.drop(['channel group ...'])     df.columns = df.columns.droplevel()     df.columns.set_levels(['all ind', 'comm demo'], 0, inplace=true)  

the excel used here: https://www.dropbox.com/s/vvpz8p7kzpcph6q/time%20kpi%20000s%20bg.xls?dl=0

fig1

enter image description here

fig 2

enter image description here

use set_levels
consider df

df = pd.dataframe(np.arange(8).reshape(-1, 4),                   columns=pd.multiindex.from_product([['a4+', 'a18-49'], list('xy')])) df 

enter image description here


m = {'a4+': 'all ind', 'a18-49': 'comm demo'} df.columns.set_levels(df.columns.levels[0].to_series().map(m), level=0, inplace=true) df 

enter image description here


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -