'In Python, can the "replace function" replace strings equal to instead containting in a dataframe?

I have a dataframe that I scraped from Yahoo Finance. I want to replace missing values (which are noted as "-") so that I can adjusted the dataframe to numeric. However, when I use the replace function, it removes the "-" from negative numbers a well. Is there a way so that I can replace only strings in my dataframe exactly equal to "-"?



Solution 1:[1]

df[col] = df[col].apply(lambda x: x.replace('-', 'what you want') if x == '-' else x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1