简体   繁体   English

使用 map 在 Pandas 数据框列上应用函数

[英]Applying a function on a pandas dataframe column using map

I am doing sentiment analysis for the first time.我是第一次做情感分析。 I am analyzing yelp reviews.我正在分析 yelp 评论。 I have converted the reviews into a list before writing them into a csv file.在将评论写入 csv 文件之前,我已将评论转换为列表。 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这会创建一个新列(newtext),但我收到此消息而不是获得干净的文本

map object at 0x000001C1B9CE07F0 0x000001C1B9CE07F0 处的映射对象

I am using python 3. Please help.我正在使用 python 3。请帮忙。 Thank you谢谢

Python's map function returns map objects, which need to be cast to lists. Python 的map函数返回 map 对象,需要将其转换为列表。 Example示例

So, you can just cast your map() call in a list()因此,您可以将map()调用转换为list()

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

map will slow things down, especially for large dataframes. map会减慢速度,尤其是对于大型数据帧。 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.流行的方法是调用str访问器方法 - encodedecode ,它们做完全相同的事情,但速度要快得多。

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

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']))

只需将地图对象转换为列表,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM