简体   繁体   中英

rename index of an aggregated dataframe

try to rename the index of the aggregated mean values for better presentation.

    mapping={
    'bbom':'BB Crossover and stay over midline',
    'bbos':'BB Crossover and MACD over signal line',
    'cao3':'Bulk trade over 3% cap',
    'maos':'Positive MACD over signal line',
    'rscp':'Oversold cheap big chips'}

an=an.reindex(columns=['date','type','stocknum','price','pe','d1','d2','d5','d10','d20'])

an_typed=an.groupby('type')
mean_overall=an_typed.mean()
print(mean_overall.index)
mean_overall.rename(index=mapping)

out:

Index(['bbom', 'bbos', 'cao3', 'maos', 'rscp'], dtype='object')
####################################################
OVERALL:

mean
           price         pe          d1          d2          d5         d10  \
type                                                                         
bbom   4.247788  25.697788  100.815980  100.350360  100.426334  100.898103   
bbos   4.760615  14.078462   99.759520   99.581404   99.821370   98.940811   
cao3   1.825135  55.514054  101.634849  100.358120   99.743545  105.610347   
maos   6.600822  17.105342  100.288234  100.578242   99.712953  100.561209   
rscp  14.990484   5.622903   99.954267   99.884227   97.935620         NaN   

      d20  
type       
bbom  NaN  
bbos  NaN  
cao3  NaN  
maos  NaN  
rscp  NaN  

[5 rows x 7 columns]

the mapping make no effect, but from the print out of the aggregated mean, the index is already based on the groupby key.

rename returns a new dataframe so if you don't assign it to itself then it will have no effect. So you need to set inplace=True in order for it to modify the dataframe.

mean_overall.rename(index=mapping, inplace=True)

See the online docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM