Update pandas series value based on condition

I have a series of lines. I want to do something like this:

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

      

Basically, if there is a '!' on a line, replace it with the part before the "!" The code doesn't seem to change the series at all. How to properly perform conditional substitution?

+3


source to share


3 answers


I think that the condition is not necessary if you use str.split

with indexing the string :

s = pd.Series(['sss!dd','sdsd', 'aa!p'])

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

      



But add mask

and if necessary str.contains

:

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

      

+5


source


Using @jezrael dataset:



In [74]: s
Out[74]:
0    sss!dd
1      sdsd
2      aa!p
dtype: object

In [75]: s = s.str.replace(r'\!.*','')

In [76]: s
Out[76]:
0     sss
1    sdsd
2      aa
dtype: object

      

+4


source


Option 1
You can also use pd.Series.replace

with the parameterregex=True

s.replace('(.*)!.*', r'\1', regex=True)

0     sss
1    sdsd
2      aa
dtype: object

      

Option 2
You can usenumpy.core.defchararray.split

pd.Series(
    [x[0] for x in np.core.defchararray.split(s.values.astype(str), '!')],
    s.index
)

0     sss
1    sdsd
2      aa
dtype: object

      

+3


source







All Articles