2
import pandas as pd
from nltk.stem import PorterStemmer, WordNetLemmatizer
porter_stemmer = PorterStemmer()

df = pd.read_csv("last1.csv",sep=',',header=0,encoding='utf-8')

df['rev'] = df['reviewContent'].apply(lambda x : filter(None,x.split(" ")))

Dataset

I am trying to stem my dataframe. While tokenizing I am getting this error for

df['rev'] = df['reviewContent'].apply(lambda x : filter(None,x.split(" ")))

AttributeError: 'float' object has no attribute 'split'

While using Stemming I also get the float problem

df['reviewContent'] = df["reviewContent"].apply(lambda x: [stemmer.stem(y) for y in x])

TypeError: 'float' object is not iterable

What can I do?

4
  • Where is your data? What is your expected output? Your code isn't enough to help. Commented Nov 7, 2017 at 16:20
  • This is a dataset for yelp fake review. I am trying to stem my whole dataset. Should I upload the dataset too?? Commented Nov 7, 2017 at 16:31
  • How about the first 5 rows instead? Commented Nov 7, 2017 at 16:31
  • I edited the post and added a photo of the dataset. Is it enough? Commented Nov 7, 2017 at 16:44

2 Answers 2

4

When tokenising your data, you don't need the apply call. str.split should do just fine. Also, you can split on multiple whitespace, so you don't have to look for empty strings.

df['rev'] = df['reviewContent'].astype(str).str.split()

You can now run your stemmer as before:

df['rev'] = df['rev'].apply(lambda x: [stemmer.stem(y) for y in x])
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry getting another error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 79-80: ordinal not in range(128) I havo to do the following things to overcome the error: import sys reload(sys) sys.setdefaultencoding('utf8') is it okay?
@AshfaqAliShafin Yeah, that's okay. All the best!
0

We can also write it this way

df['rev'] = df['rev'].astype(str).apply(lambda x: stemmer.stem(x))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.