I have a dataframe that contains in the column "Title" the following information:
Title
2.1 text1 and bla bla 1
2.2 text2 bla 2
2.3 text3 other text 7
I tried to split the column in the following manner:
col1 Title col3
2.1 text1 and bla bla 1
2.2 text2 bla 2
2.3 text3 other text 7
I tried to follow multiple ways but without success.
>Solution :
You can split on white space to obtain x. Then take the first items of lists and assign them to 'col1' and the last items to 'col3'. Finally take what’s left, join them back and assign them to 'Title':
x = df['Title'].str.split()
df['col1'] = x.str[0]
df['col3'] = x.str[-1]
df['Title'] = x.apply(lambda y: ' '.join(y[1:-1]))
df = df[['col1','Title','col3']]
Output:
col1 Title col3
0 2.1 text1 and bla bla 1
1 2.2 text2 bla 2
2 2.3 text3 other text 7