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

python - What's the Pythonic way to report nonfatal errors in a parser? -

sql server - Deadlock occuring in Clustered Columnstore index -

php - curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to domain.com:443 -