I am working with dataframe that contains column named "raw_parameter_name". In this column i have different string values. Several values are like following pattern "ABCD;MEAN". What i am trying to do is to replace each value "ABCD;MEAN" with "ABCD;X-BAR". Sub string "ABCD" may vary but pattern ";MEAN" is constant i want to replace. Looked into different options using "replace" method but don't know how to replace sub string only and not whole string. Please advise. Thank you in advance
相关问题
- How to remove spaces in between characters without
- split data frame into two by column value [duplica
- Removing duplicate dataframes in a list
- Groupby with weight
- Pandas reshape dataframe by adding a column level
相关文章
- How to convert summary output to a data frame?
- Paste all possible diagonals of an n*n matrix or d
- implementing R scale function in pandas in Python?
- Raspberry Pi-Python: Install Pandas on Python 3.5.
- How to apply multiple functions to a groupby objec
- How to remove seconds from datetime?
- 'DataFrame' object has no attribute 'i
- OLS with pandas: datetime index as predictor
You can use regex module
re
for example:use
str.contains
to create a boolean index to mask the series and thenstr.replace
to replace your substring:Here it matches where the substrin
';MEAN'
exists the$
is a terminating symbolThe boolean mask looks like the following:
Timings
For a 40,0000 row df using
str.replace
is faster than usingapply
:You do not have to use
re
like in the example that was marked correct above. It may have been necessary at one point in time, but this is not the best answer to this anymore.Nor do you need to use
str.contains()
first.Instead just use
.str.replace()
with the appropriate match and replacement.