Gingerhaze
Gingerhaze

Reputation: 706

Add suffix to column names that don't already have a suffix

I have a data frame with columns like

Name  Date  Date_x  Date_y  A  A_x  A_y..

and I need to add _z to the columns (except the Name column) that don't already have _x or _y . So, I want the output to be similar to

Name  Date_z  Date_x  Date_y  A_z  A_x  A_y...

I've tried

df.iloc[:,~df.columns.str.contains('x|y|Name')]=df.iloc[:,~df.columns.str.contains('x|y|Name')].add_suffix("_z")
# doesn't add suffixes and replaces columns with all nans

df.columns=df.columns.map(lambda x : x+'_z' if "x" not in x or "y" not in x else x) 
#many variations of this but seems to add _z to all of the column names

Upvotes: 2

Views: 690

Answers (4)

Quickbeam2k1
Quickbeam2k1

Reputation: 5437

You can also try:

df.rename(columns = lambda x: x if x=='Name' or '_' in x else x+'_z')

stealing slightly from Quang Hoang ;)

Upvotes: 1

Andy L.
Andy L.

Reputation: 25269

I would use index.putmask as follows:

m = (df.columns == 'Name') | df.columns.str[-2:].isin(['_x','_y'])
df.columns = df.columns.putmask(~m, df.columns+'_z')

In [739]: df.columns
Out[739]: Index(['Name', 'Date_z', 'Date_x', 'Date_y', 'A_z', 'A_x', 'A_y'], dty
pe='object')

Upvotes: 0

ALollz
ALollz

Reputation: 59579

Add '_z' where the column stub is duplicated and without a suffix.

m = (df.columns.str.split('_').str[0].duplicated(keep=False)
     & ~df.columns.str.contains('_'))

df.columns = df.columns.where(~m, df.columns+'_z')

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150825

How about:

df.columns = [x if x=='Name' or '_' in x else x+'_z' for x in df.columns]

Upvotes: 5

Related Questions