gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:19:06 25 4
gpt4 key购买 nike

我在使用大型 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com