问题
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
- Remove NaN elements from df['data'] or,
- 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