Iza Pziza
Iza Pziza

Reputation: 77

Turning a list of lists into a column of arrays in a dataframe

I have an embedding matrix - that is a list of same size embedding vectors. Each embedding correspond to a sentence. I have a pandas dataframe where each row represents a sentence, and I would like to add to this dataframe a new column that contains the embedding of each sentence.

I tried:

df['sent_emb'] = pd.DataFrame(sentences)

And got :

> ValueError: Wrong number of items passed 30, placement implies 1

(My embedding size is 30). I couldn't get it right without loosing the array structure.

Thanks

Upvotes: 1

Views: 621

Answers (2)

YOLO
YOLO

Reputation: 21719

In this case, you can pass the sentences as pandas series. Below is a minimal example for understanding:

df = pd.DataFrame({'aaa': ['a','b','c']})
ex = [[1,2,3],[2,3,4],4,5,6]
df['sentences'] = pd.Series(ex)
print(df)

    aaa sentences
0   a   [1, 2, 3]
1   b   [2, 3, 4]
2   c       4

Upvotes: 3

arnaud
arnaud

Reputation: 3473

You're assigning a pd.DataFrame to a pd.Series, that can't work.

Maybe try tris :

df['sent_emb'] = sentences 

Upvotes: 0

Related Questions