I have a dataframe as shown below:
data1 = {"first":["alice", "bob", "carol"],
"last_huge":["foo", "bar", "baz"]}
df = pd.DataFrame(data1)
For example, I want to replace all the characters 'o' with 'a':
Then i do
df.replace({"o":"a"},regex=True)
Out[668]:
first last
0 alice faa
1 bab bar
2 caral baz
It returns what I need.
However , when I want to replace 'o' with np.nan
, it will change the whole line to np.nan
. Are there any explanations from the pandas document? . I can find some information through the source code .
Additional information: (The whole line will be changed to np.nan
)
df.replace({"o":np.nan},regex=True)
Out[669]:
first last
0 alice NaN
1 NaN bar
2 NaN baz
source
share