Hi everyone. I have read the method ‘replace’ for DataFrames and series without accessing series.str. As far as I understood, they do the same thing as series.str.replace function and changes the strings in a column. But when I used series.replace method instead of series.str.replace, it did not do the required action. What can be the problem?
Hard to answer without knowing your code.
Did you compare your code with the Pandas documentation examples?
Yes, I have checked the documentation.
My code was
This does not result with any replacement of the column called “ram” in laptops dataframe.
However, if I use laptops[“ram”].str.replace(“GB”,””) it does what it requires. Erases all “GB” in each string of that column.
replace() method is applied to dataframes and series, and it works only in case when the whole item of that series or dataframe coincides with the indicated value to be replaced. It can actually replace also floats or integers, not only strings. For example:
s = pd.Series([1, 2, 3, 4, 5]) s.replace(5, 10) 1 1 2 2 3 3 4 4 5 10
str.replace(), instead, is applied only to the series of strings, and in each item of that series (which is a string, as we stated) it looks for a pattern to be replaced. This pattern can coincide with the whole item, or with a part of it - anyway, the coinciding part will be replaced.
In your case, you were looking for the pattern “GB”, which was a part of some strings (in your case - of each string) to be replaced with an empty string. So, in your case, it was not the whole string to be replaced, and applying
replace() method instead of
str.replace() wouldn’t cause any error, but it would result in nothing (since it wouldn’t find any stand-alone “GB” string to replace).
Building on @Elena_Kosourova’s sample and your code,
this is what I believe you were looking for:
s = pd.Series(['1', '2', '3', '4', '5GB 6GB 7GB']) s.replace('GB', '', regex=True)
0 1 1 2 2 3 3 4 4 5 6 7 dtype: object
Notice the use of keyword-parameter
The same transformation can be applied via
str accessor usage, although I find it not necessary when you can do
pd.Series.replace('....', '....', regex=True, inplace=True) on an already string column.
Thank you very much for the explanation!
Thanks for the explanation!