Error in removing punctuation: 'float' object has no attribute 'translate'

血红的双手。 提交于 2021-02-10 15:06:02

问题


I am trying to remove punctuations from a col in a data frame by doing the following:

def remove_punctuation(text):
      return text.translate(table)

df['data'] = df['data'].map(lambda x: remove_punctuation(x))

But I am getting the following error:

'float' object has no attribute 'translate'

I checked the dtype of the col as in here:

from pandas.api.types import is_string_dtype
is_string_dtype(df['data'])

and got the following output:

True

I am not sure what's going wrong in here?

I have also tried the following: translator = str.maketrans('', '', string.punctuation)

def remove_punctuation(text):
      return text.translate(translator)
df['data'] = df['data'].map(lambda x: remove_punctuation(x))

but I am still getting the same error


回答1:


Your df['data'] has NaN elements.type(np.nan) is float. Hence, you are getting an error "'float' object has no attribute 'translate'" while removing punctuations.To fix this issue, you can either

  1. Remove NaN elements from df['data'] or,
  2. Use df['data'] = df.fillna({'data':''}) to fill NaN values with empty string.

Once you have taken care of NaN elements in your pandas Series, you can use your map function to remove punctuations.

Hope this helps!



来源:https://stackoverflow.com/questions/50443494/error-in-removing-punctuation-float-object-has-no-attribute-translate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!