请不要立即标记我的答案,因为我搜索了其他几个没有解决我问题的问题,例如 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'
提前谢谢大家!
请您参考如下方法:
您可以直接从数据框中的值创建一个集:
set(df.values.ravel())
{'me', 'yes', 'it'}
