fred.schwartz
fred.schwartz

Reputation: 2155

Split Pandas column name by delimiter

I have a dataframe, with columns headers, which I want to split by a delimiter '__' and use only the first part as the column name.

Eg

start

    Red__34343    Green__485838384    Blue__3
0
1
2
3

output

    Red    Green    Blue
0
1
2
3

Upvotes: 0

Views: 2848

Answers (2)

ansev
ansev

Reputation: 30920

Use str.split:

df.columns=df.columns.str.split('__',expand=True).get_level_values(0)
df.columns
#Index(['Red', 'Green', 'Blue'], dtype='object')

or as @anky_91 suggested

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

Upvotes: 3

Quang Hoang
Quang Hoang

Reputation: 150735

You can try extract:

df.columns = df.columns.str.extract('(.*)__')[0]

output:

0  Red  Green  Blue
0    0    NaN   NaN
1    1    NaN   NaN
2    2    NaN   NaN
3    3    NaN   NaN

Upvotes: 3

Related Questions