Use keras pad_sequences in pandas dataframe

Viewed 1646

I have a pandas data frame which contains word indexes.

    id  seq                          int_sequence
0   111 cat over dog                 [2, 7, 3]
1   222 hello silly dog cat from     [6, 9, 3, 2, 5]
2   333 biscuit sandwich food        [1, 8, 4]

I am now trying to pad the sequences with zeros to the max length.

padded_sequences = pad_sequences(df.int_sequence, maxlen=MAX_SEQ_LENGTH, padding='post', value=0)

Which returns a numpy array of shape (3,5) - following which I am using the below transformation to convert the numpy array into a df and join back to the original df:

df_padded_sequences = pd.DataFrame({ 'padded_seq': padded_sequences.tolist()})

Whilst it works, it feels hacky - so I would like to pad_sequences in situ on the pandas df mainly so I am not worried about any of the orders breaking and therefore changing.

Thanks!

1 Answers

Ok, so I have made it work! Happy to hear any better solutions though...

df['padded_sequences'] = pad_sequences(df.int_sequence, maxlen=MAX_SEQ_LENGTH, padding='post', value=0).tolist()

Converting the numpy array to a list.

Related