Reputation: 531
How to convert all rows of a column to numpy array format in a pandas dataframe? A sample dataframe:
df=pd.DataFrame({
"actual":["1,0,0,1","0,0,1,0"],
"predicted":["[1,0,0,0]","[0,1,1,1]"]
})
Ideal data frame:
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(',')]))
Upvotes: 0
Views: 581
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
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