gpt4 book ai didi

Python 从 panda 列转换为列表?

转载 作者:行者123 更新时间:2023-12-01 08:14:21 24 4
gpt4 key购买 nike

我想知道我是否有以下格式的文件 enter image description here我想将每一列放入列表列表中,因为我有多个句子:所以输出看起来像这样

[['Learning centre of The University of Lahore is established for professional development.'], 
['These events, destroyed the bond between them.']]

对于动词列也是如此。这是我尝试过的,但它将所有内容放在一个列表中,而不是列表列表中

train_fn="/content/data/wiki/wiki1.train.oie"


dfE = pandas.read_csv(train_fn, sep= "\t",
header=0,
keep_default_na=False)
train_textEI = dfE['word'].tolist()
train_textEI = [' '.join(t.split()) for t in train_textEI]
train_textEI = np.array(train_textEI, dtype=object)[:, np.newaxis]

它输出列表中的每个单词

[['Learning'],['Center'],['of'],['The'],['University'],['of'],
['Lahore'],['is'],['established'],['for'],['the'],
['professional'],['development'],['.'],['These'],['events'],[','],
['destroyed'],['the'],['bond'],['between'],['them'],['.']]

最佳答案

通过将 word_idSeries.eq 进行比较来创建帮助器 SeriesSeries.cumsumgroupby并转换为列表,最后将输出Series转换为列表:

df = pd.DataFrame({'word_id':[0,1,2,0,1],
'word':['a s','ds d','sss dd','d','sd ds']})

L = df.groupby(df['word_id'].eq(0).cumsum())['word'].apply(lambda x: [' '.join(x)]).tolist()
print (L)
[['a s ds d sss dd'], ['d sd ds']]

关于Python 从 panda 列转换为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059360/

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