【发布时间】:2019-05-15 01:28:12
【问题描述】:
请不要立即标记我的答案,因为我搜索了其他几个没有解决我问题的问题,例如this.
我正在尝试从 csv 文件生成一组 python 字符串。加载的 csv 文件打印出来的 pandas 数据帧结构如下:
0
0 me
1 yes
2 it
对于一个项目,我需要将其格式化为如下所示
STOPWORDS = {'me', 'yes', 'it'}
我尝试通过以下代码做到这一点。
import pandas as pd
df_stopwords = pd.read_csv("C:/Users/Jakob/stopwords.csv", encoding = 'iso8859-15', header=-1)
STOPWORDS = {}
for index, row in df_stopwords.iterrows():
STOPWORDS.update(str(row))
print(STOPWORDS)
但是,我收到此错误:
dictionary update sequence element #0 has length 1; 2 is required
当我使用STOPWORDS.update(str(row)) 时,我得到了这个错误:
'dict' object has no attribute 'add'
提前谢谢大家!
【问题讨论】:
-
您需要
dict还是set? -
怎么样:
set(df_stopwords[0]) -
做类似
set(df.values.ravel())的事情 -
@nixon 请将其发布为答案。我想把这归功于你!解决了我的问题。总之:请点赞! ;-P
-
@YOLO 您的解决方案也很完美。我想接受你的两个答案! :D 非常感谢!