【发布时间】:2020-01-12 11:37:55
【问题描述】:
我已经尝试了很多不同的东西,我假设我在这里很近。我有一个由 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 分钟后,我应该寻求帮助。谢谢!
【问题讨论】:
-
你得到了什么输出,你想得到什么输出?
-
假设
newnlpkeywords是list而不是str类型,其中包含括号,您可以使用df["newnlpkeywords"] = df["newnlpkeywords"].apply(lambda x: ", ".join(x))。如果变量是str类型,那么你应该可以做到df["newnlpkeywords"].str.replace(r"\[|\]", "") -
我的输出实际上是列表,如 ['screened', 'model', 'health', 'volume']
-
@brittenb - 成功了,谢谢!
标签: python pandas list text replace