【发布时间】:2020-06-29 18:19:19
【问题描述】:
我的数据如下:
import pandas as pd
df2 = pd.DataFrame([[ "I am new at programming."],
[ "Leaves are falling from tree."]], columns = ['Text'])
输入文件打印:
Text
0 I am new at programming.
1 Leaves are falling from tree.
我有一个执行 NLP 任务的代码:
NewListA = []
for inputs in df2['Text']:
t = nlp(inputs)
res_A = {}
for sent in t.sentences:
for word in sent.words:
# append to dict
txt= f'{word.text}'
upos= f'{word.upos}'
res_A[txt]= upos
NewList = list(res_A.items())
NewListA.append(NewList)
输出为:
[[('I', 'PRON'), ('am', 'AUX'), ('new', 'ADJ'), ('at', 'ADP'), ('programming', 'NOUN'), ('.', 'PUNCT')], [('Leaves', 'NOUN'), ('are', 'AUX'), ('falling', 'VERB'), ('from', 'ADP'), ('tree', 'NOUN'), ('.', 'PUNCT')]]
这会产生一个额外的最外面的列表括号。我想删除最外面的括号并得到:
[('I', 'PRON'), ('am', 'AUX'), ('new', 'ADJ'), ('at', 'ADP'), ('programming', 'NOUN'), ('.', 'PUNCT')], [('Leaves', 'NOUN'), ('are', 'AUX'), ('falling', 'VERB'), ('from', 'ADP'), ('tree', 'NOUN'), ('.', 'PUNCT')]
我可以将其转换为数据框并最终得到:
POS
0 [('I', 'PRON'), ('am', 'AUX'), ('new', 'ADJ'), ('at', 'ADP'), ('programming', 'NOUN'), ('.', 'PUNCT')]
1 [('Leaves', 'NOUN'), ('are', 'AUX'), ('falling', 'VERB'), ('from', 'ADP'), ('tree', 'NOUN'), ('.', 'PUNCT')]
我查看了 this solution 或 this 但是,这些删除了列表中的所有括号,这不是我要找的。p>
注意:我的愿望结果示例是:
[['A','B'],['B','C']] --> ['A','B'],['B','C']
【问题讨论】:
-
你能给出预期的结果吗?
-
@AnnZen 谢谢。我刚刚更新了
-
@Bilgin 你确定你的代码有问题吗?删除最外面的括号不会对您的代码产生有意义的影响(请参阅下面的答案),并且您提供的示例应该已经生成了您想要的数据框。
-
@Bilgin 完美。我添加了一个答案,