John Constantine
John Constantine

Reputation: 1092

Convert Pandas Object to Array

I have a column in dataframe which is of the type object. I want to convert it to an array. ex:

'["399866273","12444645","162497334"]'

I am aware of converting it to string and splitting it based on ,. But I want to know if there is a better way to do this.

Upvotes: 0

Views: 1540

Answers (3)

iadi7ya
iadi7ya

Reputation: 142

You can try something like this:

df = pd.DataFrame(['["399866273","12444645","162497334"]'], columns = ['Column_ObjectType'])

df.dtypes
#output
Column_ObjectType    object
dtype: object

df[['Value1','Value2','Value3']] = df.Column_ObjectType.str.split(",", expand=True)
df.head()
#output
                      Column_ObjectType        Value1      Value2        Value3
0  ["399866273","12444645","162497334"]  ["399866273"  "12444645"  "162497334"]

df['Value1'] = df.Value1.apply(lambda x: x[2:-1])
df['Value2'] = df.Value1.apply(lambda x: x[1:-1])
df['Value3'] = df.Value1.apply(lambda x: x[1:-1])
df.head()
#output
                  Column_ObjectType     Value1    Value2    Value3
0  ["399866273","12444645","162497334"]  399866273  39986627  39986627

Upvotes: 1

skullgoblet1089
skullgoblet1089

Reputation: 614

import json
import numpy as np

def toarray(s):
    x = json.loads(s)
    arr = np.array(x)
    return arr

# Test
s = '["399866273","12444645","162497334"]'
asset type(toarray(s)) == np.ndarray
assert all(toarray(s) == np.array(["399866273", "12444645", "162497334"]))

# Apply to df
colname = ...  # your col name
df['as_array'] = df[colname].apply(toarray)

Upvotes: 1

atszet
atszet

Reputation: 1

If you mean a list you can just use eval()

Upvotes: 0

Related Questions