This works using double [[]] , you can access the index value of the current element so that you can index into a series of split_locations :
In [119]: df[['group_class']].apply(lambda x: pd.Series([x.str[split_locations[x.name]:][0], x.str[:split_locations[x.name]][0]]), axis=1) Out[119]: 0 1 0 class1 group9 1 class2 group10 2 class20 group11
Or, since @ajcr suggested that you can extract :
In [106]: df['group_class'].str.extract(r'(?P<group>group[0-9]+)(?P<class>class[0-9]+)') Out[106]: group class 0 group9 class1 1 group10 class2 2 group11 class20
EDIT
Regex explanation:
the regular expression came from @ajcr (thanks!), it uses str.extract to extract the groups, the groups become new columns.
So ?P<group> here identifies the identifier for a specific group to look for, if it is missing, an int will be returned for the column name.
so the rest should be self-evident: group[0-9] looks for the string group followed by numbers in the range [0-9] , which indicates [] , this is equivalent to group\d , where \d means a digit.
Therefore, it can be rewritten as:
df['group_class'].str.extract(r'(?P<group>group\d+)(?P<class>class\d+)')
source share