如何从Python中的DataFrame中提取单词?

时间:2017-09-15 16:15:59

标签: python pandas

我有一个DataFrame(B),每列有两列500字。我尝试创建一个仅包含(B)中找到的唯一单词的新列表(C):因此(B)中的每个单词在(C)中只出现一次。

但是,抛出以下错误,我无法解决:“ValueError:DataFrame的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。“

有什么建议吗?这是我的代码:

import pandas as pd
q = "questions.tsv"
data = pd.read_csv(q, usecols = [3, 4], nrows = 9, header=0, sep="\t")
first_words = []
for word in data:
    first_words.append(data.applymap(lambda x: x.split()[0]))
unique_words = []
for w in first_words:
    if w not in unique_words:
        unique_words.append(w)
print(unique_words)

Column 1. Column 2
0 What      What
1 What      What
2 How       How
3 Why       Find
4 Which    Which
5 Should   What
6 How      What
7 When      When

 I would expect is to get a list (C) like this:
What
How
Why
Find
Which
Should
When

0 个答案:

没有答案