Why can't I rename 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 as follows:

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

Basically remove from column names .SKand.LO

Here is what I tried:

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

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

This second case works fine in the case of df.index

+4
source share
2 answers

DataFrame.rename () does NOT change the DataFrame in place (by default), so you need 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
+5
source

I think it’s faster if there are a lot of columns, a vectorized solution is used with str.split, and then select the first one listsonstr[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
+1
source

Source: https://habr.com/ru/post/1676601/


All Articles