I have a dataframe as below :
data1 = {"first":["alice", "bob", "carol"],
"last_huge":["foo", "bar", "baz"]}
df = pd.DataFrame(data1)
For example , I want to replace all character 'o' to 'a':
Then I do
df.replace({"o":"a"},regex=True)
Out[668]:
first last
0 alice faa
1 bab bar
2 caral baz
It give back what I need .
However, when I want to replace 'o' to np.nan
, It will change entire string to np.nan
. Is there any explanation from pandas' document? I can find some information through the source code .
More Information:(It will change whole string to np.nan
)
df.replace({"o":np.nan},regex=True)
Out[669]:
first last
0 alice NaN
1 NaN bar
2 NaN baz
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…