I have a data frame look like this:
JavaScript
x
3
1
data = {'ID':['DFSADFEFDSAE','FDSADFDSEFDSAFEFDSADFE','ESADFDSADFSADFSA']}
2
data = pd.DataFrame(data)
3
I want to find all 'E'
in each string and save the indices into another column. I was trying with re.finditer
and map to convert list to a string and save for each row but no luck yet. What would be a good approach?
Advertisement
Answer
This will work:
JavaScript
1
4
1
import re
2
data['new']=[[m.start() for m in re.finditer('E', i)] for i in data['ID']]
3
print(data)
4
Output:
JavaScript
1
5
1
ID new
2
0 DFSADFEFDSAE [6, 11]
3
1 FDSADFDSEFDSAFEFDSADFE [8, 14, 21]
4
2 ESADFDSADFSADFSA [0]
5