JamesHudson81
JamesHudson81

Reputation: 2273

Why I can't rename the columns?

I have the following df:

          TAN.SK    SHA.LO
A         0.05      0.01   
S         0.04      0.44
D         0.08     -0.18

I would like the new df to be like:

          TAN        SHA
A         0.05      0.01   
S         0.04      0.44
D         0.08     -0.18

Basically remove from the column names .SK and .LO

This is what I have tried:

df.rename(columns=lambda x: x.split('.')[0])

df.columns=df.split('.')[0]

This second case works perfectly in case of df.index

Upvotes: 6

Views: 7161

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

DataFrame.rename() does NOT change the DataFrame in place (per default), so you have to assign it back:

In [134]: df = df.rename(columns=lambda x: x.split('.')[0])

In [135]: df
Out[135]:
    TAN   SHA
A  0.05  0.01
S  0.04  0.44
D  0.08 -0.18

or

In [139]: df.rename(columns=lambda x: x.split('.')[0], inplace=True)

In [140]: df
Out[140]:
    TAN   SHA
A  0.05  0.01
S  0.04  0.44
D  0.08 -0.18

Upvotes: 6

jezrael
jezrael

Reputation: 863741

I think faster, if many columns, is use vectorized solution with str.split and then select first lists by str[0]:

print (df.columns.str.split('.'))
Index([['TAN', 'SK'], ['SHA', 'LO']], dtype='object')

df.columns = df.columns.str.split('.').str[0]
print (df)
    TAN   SHA
A  0.05  0.01
S  0.04  0.44
D  0.08 -0.18

Upvotes: 1

Related Questions