简体   繁体   中英

How to rename a pandas DataFrame index based on a column condition

I have a DataFrame like this:

import pandas as pd
df = pd.DataFrame(data= {"x": [1,2,3,4],"y":[5,6,7,8],"i":["a.0","a.1","a.0","a.1"]}).set_index("i")
df

Out:

     x  y
i        
a.0  1  5
a.1  2  6
a.0  3  7
a.1  4  8

and I want to rename the index based on a column condition:

df.loc[df["y"]>6].rename(index=lambda x: x+ ">6" )

what gives me:

       x  y
i    
a.0>6  3  7
a.1>6  4  8

I tried it with inplace=True, but it does not work

df.loc[df["y"]>6].rename(index=lambda x: x+ ">6" , inplace=True )

I only could get it done by resetting the index, changing the i-column-values via apply and set the index again:

df1 = df.reset_index()
df1.loc[df1["y"]>6, "i"] = df1.loc[df1["y"]>6, "i"].apply(lambda x: x+ ">6" )
df1.set_index("i", inplace=True)
df1

Out:

       x  y
i    
a.0    1  5
a.1    2  6
a.0>6  3  7
a.1>6  4  8

But this is so complicated. Do you know if there is an easier way?

How about trying this?

import numpy as np
df.index=np.where(df['y']>6, df.index+'>6', df.index)

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