I'm looking to split a string Series at different points depending on the length of certain substrings:
In [47]: df = pd.DataFrame(['group9class1', 'group10class2', 'group11class20'], columns=['group_class'])
In [48]: split_locations = df.group_class.str.rfind('class')
In [49]: split_locations
Out[49]:
0 6
1 7
2 7
dtype: int64
In [50]: df
Out[50]:
group_class
0 group9class1
1 group10class2
2 group11class20
My output should look like:
group_class group class
0 group9class1 group9 class1
1 group10class2 group10 class2
2 group11class20 group11 class20
I half-thought this might work:
In [56]: df.group_class.str[:split_locations]
Out[56]:
0 NaN
1 NaN
2 NaN
How can I slice my strings by the variable locations in split_locations
?