Str.replace method and dataframe or series.replace

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?

Hi @shhuseynli2018,

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

laptops[“ram”]=laptops[“ram”].replace(“GB”,””)
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.

1 Like

Hi @shhuseynli2018,

The 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

The method 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).

5 Likes

Hello @shhuseynli2018,

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)

Output:

0        1
1        2
2        3
3        4
4    5 6 7
dtype: object

Notice the use of keyword-parameter regex=True?

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.

2 Likes

Thank you very much for the explanation!

1 Like

Thanks for the explanation!

1 Like