0

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

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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(',')]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.