1

The is my pandas data frame, In the index column i want to keep only the values after double underscore(__) and remove the rest.

enter image description here

2 Answers 2

3

Use str.split with parameter n=1 for split by first splitter (if possible multiple __) and select second lists:

df['index'].str.split('__', n=1).str[1]

Or use list comprehension if no missing values and performance is important:

df['last'] = [x.split('__', 1)[1] for x in df['index']]
Sign up to request clarification or add additional context in comments.

4 Comments

Why is a list comprehension a good alternative for string operations? For more information, see my writeup here.
It works fine. Along with the index field, i just want to get all the columns? Any way?
@ShankarPanda - So need df1 = df.join(df['index'].str.split('__', expand=True)) ?
perfect !! Thanks
1

df['index'].apply(lambda x: x.split('__')[-1]) will do the trick

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.