Reputation: 113
I am trying to replace a part of all strings in pd.Dataframe, but it doe s not work.
My data example:
HLAA0101
HLAA0201
HLAA0202
HLAA0203
HLAA0205
What am I trying to obtain:
A0101
A0201
A0202
A0203
A0205
My code:
mhc = train_csv.mhc
for i in mhc:
i[0:2].replace('HLA', ' ')
print(mhc)
But it does not work.
Upvotes: 3
Views: 137
Reputation: 210822
Option 1:
df['mhc'] = df['mhc'].str[3:]
Option 2:
df['mhc'] = df['mhc'].str.replace(r'^HLA','')
Option 3:
df['mhc'] = df['mhc'].str.extract(r'HLA(.*)', expand=False)
Option 4: (NOTE: sometimes list comprehension works faster than internal vectorized methods for string/object dtypes)
df['mhc'] = [s[3:] for s in df['mhc']]
All options yield the same result:
In [26]: df
Out[26]:
mhc
0 A0101
1 A0201
2 A0202
3 A0203
4 A0205
Timing for 50.000 rows DF:
In [29]: df = pd.concat([df] * 10**4, ignore_index=True)
In [30]: %timeit df['mhc'].str[3:]
35.9 ms ± 3.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [31]: %timeit df['mhc'].str.replace(r'^HLA','')
162 ms ± 3.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [32]: %timeit df['mhc'].str.extract(r'HLA(.*)', expand=False)
164 ms ± 4.87 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [33]: %timeit [s[3:] for s in df['mhc']]
14.6 ms ± 18.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [34]: df.shape
Out[34]: (50000, 1)
Conclusion: list comprehension approach won.
Upvotes: 4
Reputation: 4200
Try str.replace
.
(Documentation here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html)
Also, in your code it inserts a space instead of the replaced strings. If you want that, that's fine, otherwise delete the space. ;)
Upvotes: 1
Reputation: 9081
Use -
mhc = mhc.str.replace('HLA', ' ')
OR -
train_csv['mhc'] = train_csv['mhc'].str.replace('HLA', '') # One liner directly from df
Upvotes: 1