python - Update pandas series value based on a condition -


i have series of strings. want this:

for item in series:     if '!' in item:         series[item] = item.split('!')[0] 

basically, if there's '!' in string, replace part before '!'. code doesn't seem change series @ all. how conditional replace properly?

i think condition not necessary if use str.split indexing str:

s = pd.series(['sss!dd','sdsd', 'aa!p'])  s = s.str.split('!').str[0] 0     sss 1    sdsd 2      aa dtype: object 

but if need condition add mask , str.contains:

s = s.mask(s.str.contains('!'), s.str.split('!').str[0]) print (s) 0     sss 1    sdsd 2      aa dtype: object 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -