[英]Replacing the corresponding column values in Python Pandas dataframe
[英]Python - Pandas replacing dataframe column values - column data stored as list (i.e., '[this, that,'])
我已经尝试了很多不同的方法,并且我假设我已经靠近这里了。 我有一个列表,这些单词是我根据Gensim关键字摘要生成器从研究摘要中生成的。 数据是准确的,但是它存储为每一行的列表,我想摆脱每一行的['和']。 我尝试了下面的代码以及其他变体,但是我遇到了错误或代码处理过程,但是没有替换。 我试过了:
#scenario 1
keywords = ['screened', 'model', 'health', 'volume']
df['newnlpkeywords'] = keywords
df['newnlpkeywords'].replace("']", "", inplace=True)
和
#scenario 2
keywords = ['screened', 'model', 'health', 'volume']
df['newnlpkeywords'] = keywords.replace(replace("']", "")
我知道这是一个菜鸟问题,但我正在尝试学习! 经过30分钟的尝试,我认为应该寻求帮助。 谢谢!
这是您要找的东西吗
import numpy as np
import re
rgx = lambda x: re.sub("']","",x)
rgx = np.vectorize(rgx)
df['newnlpkeywords'].values = rgx(df['newnlpkeywords'].values)
以下代码将rgx函数应用于df ['newnlpkeywords']中的每一行
(我知道可能有更多的Python方式可以做到这一点,但这是一个快速解决方案,我敢肯定会有一个更简洁的答案)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.