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

Shankar Panda
  • 682
  • 3
  • 10
  • 25

2 Answers2

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']]
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
1

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

Chris
  • 27,139
  • 3
  • 23
  • 44