Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

pandas - 将Pandas数据框中的列表列扩展为单列的快速方法(Fast way to expand a column of lists in pandas data frame to a single column)

I have a data frame with text and sentiment score corresponding to it.

(我有一个数据框,其中包含与之对应的文本和情感评分。)

I've created a column which stores all the bigrams in a column.

(我创建了一个列,将所有二元组存储在一个列中。)

Now I want to create a Dataframe which has this bigram column expanded with the score against it, when I do the second step using a for loop it's painfully slow

(现在,我想创建一个数据框,将这个二字列扩展为其分数,当我使用for循环执行第二步时,它的速度很慢)

enter image description here

(在此处输入图片说明)

enter image description here

(在此处输入图片说明)

  ask by White Walker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pandas >= 0.25 You can use explode .

(大熊猫> = 0.25可以使用explode 。)

df = df.explode('bigrams')

Dummy Example:

(虚拟示例:)

import pandas as pd
df1 = pd.DataFrame({'score':[0.2,0.3],
               'bigrams':[['a', 'b', 'c', 'e'],['f','g']]})

print(df1)

=========================

(========================)

df1:

(df1:)

    score   bigrams
0   0.2     [a, b, c, e]
1   0.3     [f, g]

===========================

(==========================)

df1 = df1.explode('bigrams')
print(df1)

=============================

(============================)

df1:

(df1:)

    score   bigrams
0   0.2     a
0   0.2     b
0   0.2     c
0   0.2     e
1   0.3     f
1   0.3     g

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...