0

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.

7
  • 1
    If you mean a numpy.ndarray then you just want my_df['column'].values, if you want a list, then you just want my_df['column'].to_list() Commented Jul 30, 2020 at 21:23
  • I can't do this, it would say 'str' object has no attribute 'values' @juanpa.arrivillaga Commented Jul 30, 2020 at 21:27
  • Then you don't have a dataframe you have str. How did you produce this string? You need to provide a minimal reproducible example. Commented Jul 30, 2020 at 21:30
  • Try dir(SomeObject) for the object you are getting. It may help determine what you're dealing with. Commented Jul 30, 2020 at 21:31
  • @Mike67 or just type(my_object) Commented Jul 30, 2020 at 21:34

3 Answers 3

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

Comments

1

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

Comments

0

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

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.