change column name pandas

  • A+
Category:Languages

Is it possible to change the name of a column in a pandas data frame if it starts with a certain word.

e.e If column starts with DEP then change the full name to KEEP.

[col for col in df if col.startswith('DEP') then KEEP]. 

 


Is it possible, but not recommended, because get duplicated columns names:

df = pd.DataFrame({         'DEP1':list('abcdef'),          'B':[4,5,4,5,5,4],          'C':[7,8,9,4,2,3],          'DEP2':list('aaabbb') })  print (df)   DEP1  B  C DEP2 0    a  4  7    a 1    b  5  8    a 2    c  4  9    a 3    d  5  4    b 4    e  5  2    b 5    f  4  3    b  df.columns = ['KEEP' if col.startswith('DEP') else col for col in df] print (df)   KEEP  B  C KEEP 0    a  4  7    a 1    b  5  8    a 2    c  4  9    a 3    d  5  4    b 4    e  5  2    b 5    f  4  3    b 

So then if select column KEEP it return all duplicated columns in DataFrame:

print (df['KEEP'])   KEEP KEEP 0    a    a 1    b    a 2    c    a 3    d    b 4    e    b 5    f    b 

So if want filter all columns starting with DEP use filter with regex ^ for starting string:

df1 = df.filter(regex='^DEP') #alternative solution #df1 = df.loc[:, df.columns.str.startswith('DEP')] print (df1)   DEP1 DEP2 0    a    a 1    b    a 2    c    a 3    d    b 4    e    b 5    f    b 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: