I have a df:
string word
0 anfhfd f
1 rnvkds v
2 bkfsgk k
Code:
import pandas as pd
df = pd.DataFrame( {'string':['anfhfd', 'rnvkds', 'bkfsgk'],
'word':['f', 'v', 'k'] } )
I need to split the first word in column ‘string’, so I tried:
df[['want', 'notwant']] = df['string'].str.split(df['word'].values[0], n=1, expand=True)
result:
string word want notwant
0 anfhfd f an hfd
1 rnvkds v rnvkds None
2 bkfsgk k bk sgk
The result above is not what I want. It seems like it splits only ‘f’ in each row. My expected result is:
string word want notwant
0 anfhfd f an hfd
1 rnvkds v rn kds
2 bkfsgk k b fsgk
Is there any good way to split the word and only keep the column ‘want’? I don’t need the column ‘notwant’