最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pandas - using DataFrame.replace() for replacing a string with NaN in a DataFrame.map() function returns TypeError - Stack Overf

programmeradmin1浏览0评论

I realize there are working alternatives to this, I just want to understand what is going on for my own edification or anyone else who comes across this.

df_test = pd.DataFrame({'test1':['blah1','blah2','blah3'],'test2':['blah1','blah2','blah3']})

When I run the below code on this above DataFrame, I get TypeError: replace() argument 2 must be str, not float

df_test.map(lambda x: x.replace('blah1',np.nan))

What exactly prevents 'argument 2' from being np.nan from working when of course the below works no problem

df_test.replace('blah1',np.nan)

Thanks in advance

I realize there are working alternatives to this, I just want to understand what is going on for my own edification or anyone else who comes across this.

df_test = pd.DataFrame({'test1':['blah1','blah2','blah3'],'test2':['blah1','blah2','blah3']})

When I run the below code on this above DataFrame, I get TypeError: replace() argument 2 must be str, not float

df_test.map(lambda x: x.replace('blah1',np.nan))

What exactly prevents 'argument 2' from being np.nan from working when of course the below works no problem

df_test.replace('blah1',np.nan)

Thanks in advance

Share Improve this question asked 2 days ago Casey CushingCasey Cushing 716 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Since map() is applied element-wise, each element (x) is a string- when replace() is called, it is invoking Python's built-in string method, instead of Panda's.

The error is raised because Python's built-in method requires both arguments to be strings.

you should only change the "map" function to "apply" function, as:

df_test.apply(lambda x: x.replace('blah1',np.nan))

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论