Alicia_2024
Alicia_2024

Reputation: 531

How to convert a column with list of numbers to np.array format in Pandas

How to convert all rows of a column to numpy array format in a pandas dataframe? A sample dataframe: enter image description here

df=pd.DataFrame({
        "actual":["1,0,0,1","0,0,1,0"],
        "predicted":["[1,0,0,0]","[0,1,1,1]"]
    })

Ideal data frame:

enter image description here

I tried to convert the actual column to array format using the code below but failed.

df['actual']=df.actual(lambda x: np.array([int(s) for s in x.to_numpy().split(',')]))

enter image description here

Upvotes: 0

Views: 581

Answers (2)

spo
spo

Reputation: 349

You are missing the 'apply' function in the series. And you don't have to call .to_numpy()

df['actual']=df.actual.apply(lambda x: np.array([int(s) for s in x.split(',')]))

Upvotes: 1

azro
azro

Reputation: 54148

The error comes because of df.actual( you call on the column itself, like df['actual'](, you may use Series.apply and to_numpy doesn't exists on a str

df['actual'] = df.actual.apply(lambda x: np.array([int(s) for s in x.split(',')]))

         actual  predicted
0  [1, 0, 0, 1]  [1,0,0,0]
1  [0, 0, 1, 0]  [0,1,1,1]

Upvotes: 1

Related Questions