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
  • 736
  • 3
  • 11
  • 27

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
  • 822,522
  • 95
  • 1,334
  • 1,252
1

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

Chris
  • 29,127
  • 3
  • 28
  • 51