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?
source to share
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
source to share
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
source to share