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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -