python - 如何将列表函数应用于pandas df中的文本生成器obj
问题描述
我正在将“列表”函数应用于包含生成器对象的 pandas col,以尝试在 col 中显示所有生成器对象。应用时,col 返回空列表。“subject_verb_object_triples”是一个文本功能(https://chartbeat-labs.github.io/textacy/_modules/textacy/extract.html)
打印(sp500news3)
date_publish title
79944 2007-01-29 19:08:35 <generator object subject_verb_object_triples at 0x1a42713550>
181781 2007-12-14 19:39:06 <generator object subject_verb_object_triples at 0x1a42713410>
213175 2008-01-22 11:17:19 <generator object subject_verb_object_triples at 0x1a427135f0>
93554 2008-01-22 18:52:56 <generator object subject_verb_object_triples at 0x1a427135a0>
In []: sp500news3["title"].apply(list)
Out []: 79944 []
181781 []
213175 [] ...
预期的输出是元组,如下所示:
[(Sky proposal, is, matter), (Sky proposal, is, Mays spokesman)],
[(Women, lag, Intel report)],
[(Amazon, expected, to unveil)],
[(Goldman Sachs, raising, billion)],
[(MHP, opens, books)],
[(Disney, hurls, magic), (Disney, hurls, moolah)],
[(Amazon, offering, loans), (Amazon, offering, to)], ....
如何在我的数据框中显示预期的输出?
解决方案
我已经测试了下面的代码,它工作正常
import textacy
import pandas as pd
from textacy import preprocessing
pd.options.display.max_colwidth=-1
df['<New Column name'>]=df['<Your column name that needs to be processed>'].apply(lambda x:preprocessing.normalize_whitespace(preprocessing.remove_punctuation(str(x))))
推荐阅读
- apache-spark - 如何使用Java连接到spark中的hive数据库
- macos - MacOS 无法将 localhost 解析为 127.0.0.1(忽略主机/apache 虚拟主机)
- opencv - 如何获得对象的角度并在其上投影 3d 平面?
- java - 使用 CSV 超级联赛比赛数据更新对象值 java 并返回 CSV
- html - 如何在多级弹出菜单中的某些级别上启用垂直滚动
- video-streaming - 如何播放 .m3u8 和管理缓冲?
- php - 使用 php 将 base64 写入文件时出现错误 403
- java - 飞路设置问题
- javascript - 内部函数/箭头函数中使用的“参数”行为不同
- python - BeautifulSoup 路径优化