Home > Blockchain >  How to I keep a list in a numpy array as a list when transforming it to a pandas dataframe?
How to I keep a list in a numpy array as a list when transforming it to a pandas dataframe?

Time:02-01

I think I have an easy problem, but can't find a solution.

I have an array X_train with a list of strings

4    [visa, card, geldanlage, 74843e]
Name: Keyword_clean, dtype: object

Then I want to transform this to a pandas dataframe. I use the following code:

X_train = pd.DataFrame(data=X_train, columns = ['Keyword_clean'])
X_train

The X_train dataframe then looks like this

Index Keyword_clean
4 visa,card,geldanlage,74843e

What I would like to achieve is, that it looks like this (the list of the array is still kept)

Index Keyword_clean
4 [visa,card,geldanlage,74843e]

Any ideas? thanks a lot

CodePudding user response:

You could convert it to a pd.Series first:

import pandas as pd
array =  [["visa", "card", "geldanlage", "74843e"]] * 10
df = pd.DataFrame(pd.Series(array))
  •  Tags:  
  • Related