python - Pandas:如何从每行的一个单词重建字符串

标签 python string pandas dataframe nlp

我在使用大型 Pandas DataFrame(1 500 000 行)重建句子时遇到问题。我的目标是将单词中的句子重建为新的数据帧,以便每行一个句子。我的数据框中有两个系列:单词和标签。每个句子都用感叹号分隔。除此之外,我想使用原始 DataFrame 中的标签在新的 DataFrame 中为形容词和名词/动词创建两个单独的系列。这就是我所拥有的:

>df

word    tag

bike    NOUN
winner  NOUN
!       PUNCTUATION
red     ADJECTIVE
car     NOUN
is      VERB
fast    ADJECTIVE
!       PUNCTUATION
...     ...

这就是我想要的

>df2

sent             nounverb     adj

bike winner      bike winner  None
red car is fast  car is       red fast
...

我一直无法找到解决方案,而且由于我是 Python 初学者,我无法想出一个 for 循环 来为我做到这一点.

编辑:

感谢 Andy 和 Jesús 的快速解答。安迪的回答很有效,尽管在创建新的数据帧时我需要稍作修改。需要将单词称为字符串。

df2 = pd.DataFrame({
          "sent": g.apply(lambda sdf: " ".join(sdf.word.astype(str))),
          "nounverb": g.apply(lambda sdf: " ".join(sdf[sdf.is_nounverb].word.astype(str))),
          "adj": g.apply(lambda sdf: " ".join(sdf[sdf.tag == "ADJECTIVE"].word.astype(str)))
  })

最佳答案

如果为 is“nounverb”添加虚拟列,则可以使用普通的 ol' groupby:

In [11]: df["is_nounverb"] = (df.tag == "NOUN") | (df.tag == "VERB")

然后你可以数一下你见过的!来枚举句子:

In [12]: df["sentence"] = (df.word == "!").cumsum()

In [13]: df = df[df.word != "!"]

In [14]: df
Out[14]:
     word        tag  sentence  is_nounverb
0    bike       NOUN         0         True
1  winner       NOUN         0         True
3     red  ADJECTIVE         1        False
4     car       NOUN         1         True
5      is       VERB         1         True
6    fast  ADJECTIVE         1        False

然后分组:

In [15]: g = df.groupby("sentence")

In [16]: g.apply(lambda sdf: " ".join(sdf.word))
Out[16]:
sentence
0        bike winner
1    red car is fast
dtype: object

In [17]: g.apply(lambda sdf: " ".join(sdf[sdf.is_nounverb].word))
Out[17]:
sentence
0    bike winner
1         car is
dtype: object

In [18]: g.apply(lambda sdf: " ".join(sdf[sdf.tag == "ADJECTIVE"].word))
Out[18]:
sentence
0
1    red fast
dtype: object

一起:

In [21]: df2 = pd.DataFrame({
              "sent": g.apply(lambda sdf: " ".join(sdf.word)),
              "nounverb": g.apply(lambda sdf: " ".join(sdf[sdf.is_nounverb].word)),
              "adj": g.apply(lambda sdf: " ".join(sdf[sdf.tag == "ADJECTIVE"].word))
      })

In [22]: df2
Out[22]:
               adj     nounverb             sent
sentence
0                   bike winner      bike winner
1         red fast       car is  red car is fast

关于python - Pandas:如何从每行的一个单词重建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50024431/

相关文章:

Python 3 - 如何从高维数据制作马赛克图?

python - Pandas Dataframe检查id在时间间隔内是否出现大于1

python - http.client.HTTPConnection 的 close() 函数不起作用?

python - 有没有更好的方法来查询一个字典中的值,其键是另一个字典中的值?

python - pandas dataframe 聚合固定数量的行

Python:当 False 时继续执行脚本的 if/elif 函数?

javascript - 想要在 javascript 中编写正则表达式,它将检查所有提到的字符是否至少存在

C# 文本对齐

java - 邮件内容需要用字典里的词过滤掉

python - 通过列(字符串)中的唯一元素分解 pandas 数据框并创建列联表?