Analytics beginner
Analytics beginner

Reputation: 31

Applying a function on a pandas dataframe column using map

I am doing sentiment analysis for the first time. I am analyzing yelp reviews. I have converted the reviews into a list before writing them into a csv file. I am having some coding issues with these reviews so I am running this code.

df['newtext'] = map(lambda x: x.decode('latin-1').encode('ascii','ignore'),df['comment'])

This creates a new column(newtext) but instead of getting clean text I am getting this message

map object at 0x000001C1B9CE07F0

I am using python 3. Please help. Thank you

Upvotes: 2

Views: 5822

Answers (4)

cs95
cs95

Reputation: 402844

map will slow things down, especially for large dataframes. You should know string columns offer vectorized methods which are much faster than maps and loops.

The pandaic way would be to call the str accessor methods - encode and decode, which do the exact same thing, but much faster.

df['newtext'] = df.comments.str.decode('latin-1').str.encode('ascii','ignore')

Upvotes: 2

rma
rma

Reputation: 1958

Python's map function returns map objects, which need to be cast to lists. Example

So, you can just cast your map() call in a list()

df['newtext'] = list(map(lambda x: x.decode('latin-1').encode('ascii','ignore'),df['comment']))

Upvotes: 2

Max
Max

Reputation: 1363

just convert the map object into a list as shown below

df['newtext'] = list(map(lambda x: x.decode('latin-1').encode('ascii','ignore'),df['comment']))

Upvotes: 0

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

Try this. It converts the map object to a list.

df['newtext'] = list(map(lambda x: x.decode('latin-1').encode('ascii','ignore'),df['comment']))

Upvotes: 0

Related Questions