RustyShackleford
RustyShackleford

Reputation: 3667

How to keep only the last part of column name regardless of the length of the columns?

I have df columns that looks like this:

string1_0_string2_string3          string2_0_0_1_string4
      1                                     1 

I want to only keep the name after the last underscore _ in the column name, how would I do this?

New df should look like this:

string3        string4
  1                1 

Upvotes: 1

Views: 305

Answers (1)

BENY
BENY

Reputation: 323226

Using str.split

df.columns=df.columns.str.split('_').str[-1]
df
Out[183]: 
   string3  string4
0        1        1

Upvotes: 3

Related Questions