Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - 用np.nan替换问号(Replacing question marks with np.nan)

I am trying to replace question marks in my data set with np.nan:

(我试图用np.nan替换数据集中的问号:)

I tried using the following code:

(我尝试使用以下代码:)

df['Workclass'] = [row if row!='?' else np.nan for row in df['Workclass']]

And this:

(和这个:)

df['Workclass'] = df['Workclass'].map(lambda x: np.nan if x=="?"  else x)

And this:

(和这个:)

df['Workclass'] = df['Workclass'].replace(to_replace =['?'], value = np.nan, regex = True)

But none of these solutions seem to change the frequency of question marks in the column.

(但是这些解决方案似乎都不能改变列中问号的频率。)

在此处输入图片说明

  ask by Murtaza Kamal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

(尝试这个:)

df['Workclass'].apply(lambda x: np.nan if x == '?' else x) 

and if that works:

(如果可行:)

df['Workclass'] = df['Workclass'].apply(lambda x: np.nan if x == '?' else x) 

if your looking for '?'

(如果您要查找“?”)

anywhere in a string you can use this:

(您可以在字符串中的任何位置使用此命令:)

df['Workclass'].apply(lambda x: np.nan if str(x).find('?')>-1 else x)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...