roudan
roudan

Reputation: 4262

how to change column name using apply()

I'd like to rename column name and I define a function. I am thinking how to apply this function to whole columns names.

def fix_wellname(well):
    well_num=int(well.split('A')[0])
    pad_name=well.split('A')[1]
    
    well_name_fixed=str(well_num)+'A'+pad_name
    return well_name_fixed

if I do like below:

df.columns=df.columns.apply(fix_wellname)

AttributeError: 'Index' object has no attribute 'apply'

Thanks for help

Upvotes: 0

Views: 41

Answers (1)

Grismar
Grismar

Reputation: 31436

How about:

df.columns = [fix_wellname(c) for c in df.columns]

Or if mapping the function is somehow a requirement, try:

df.columns = map(fix_wellname, df.columns)

Upvotes: 1

Related Questions