The is my pandas data frame, In the index column i want to keep only the values after double underscore(__) and remove the rest.
Asked
Active
Viewed 83 times
2 Answers
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
-
1Why is a list comprehension a good alternative for string operations? For more information, see my writeup [here](https://stackoverflow.com/questions/54028199/for-loops-with-pandas-when-should-i-care). – cs95 Jan 18 '19 at 07:33
-
It works fine. Along with the index field, i just want to get all the columns? Any way? – Shankar Panda Jan 18 '19 at 07:37
-
@ShankarPanda - So need `df1 = df.join(df['index'].str.split('__', expand=True))` ? – jezrael Jan 18 '19 at 07:38
-
perfect !! Thanks – Shankar Panda Jan 18 '19 at 07:39