Dataframe Split Text into new column

SOK :

I have a dataframe awith the following column MENU_HINT and im looking to make a new column by picking out the third word:

MENU_HINT
AUS / Cant (AUS) 
AUS / Darw (AUS)
AUS / YarV (AUS)
AUS / Goul (AUS)

My code is the following:

splittext = str(dfresults['MENU_HINT'])
dfresults['City'] = splittext.split()[3]

Which Gives Me:

MENU_HINT                City
AUS / Cant (AUS)         Cant
AUS / Darw (AUS)         Cant
AUS / YarV (AUS)         Cant
AUS / Goul (AUS)         Cant

I'm after Cant, Darw, YarV, Goul but it is fixing on the top value (Cant).

I know im close but cant seem to figure out the issue (im new to python) so any help would be great!

Thanks!

jezrael :

Use Series.str.split with slicing:

#fourth value
dfresults['City'] = dfresults['MENU_HINT'].str.split().str[3]

#third value
dfresults['City'] = dfresults['MENU_HINT'].str.split().str[2]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=408253&siteId=1