3

I have a dataframe with colums header made up of 3 tags which are split by '__'

E.g

   A__2__66    B__4__45
0
1
2
3
4
5

I know I cant split the header and just use the first tag with this code; df.columns=df.columns.str.split('__').str[0]

giving:

   A    B
0
1
2
3
4
5

Is there a way I can use a combination of the tags, for example 1 and 3.

giving

   A__66    B__45
0
1
2
3
4
5

I've trided the below but its not working

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

4 Answers 4

4

With specific regex substitution:

In [124]: df.columns.str.replace(r'__[^_]+__', '__')                                                                          
Out[124]: Index(['A__66', 'B__45'], dtype='object')
Sign up to request clarification or add additional context in comments.

Comments

3

Use Index.map with f-strings for select first and third values of lists:

df.columns = df.columns.str.split('__').map(lambda x: f'{x[0]}__{x[2]}')
print (df)
   A__66  B__45
0    NaN    NaN
1    NaN    NaN
2    NaN    NaN
3    NaN    NaN
4    NaN    NaN
5    NaN    NaN

1 Comment

@Erfan - yop, if need last value of lists then it is necessary.
2

Also you can try split and join:

df.columns=['__'.join((i[0],i[-1])) for i in df.columns.str.split('__')]
#Columns: [A__66, B__45]

Comments

2

I found your own solution perfectly fine, and probably most readable. Just needs a little adjustment

df.columns = df.columns.str.split('__').str[0] + '__' + df.columns.str.split('__').str[-1]
Index(['A__66', 'B__45'], dtype='object')

Or for the sake of efficiency, we do not want to call str.split twice:

lst_split = df.columns.str.split('__')
df.columns = lst_split.str[0] + '__' + lst_split.str[-1]
Index(['A__66', 'B__45'], dtype='object')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.