0

I have data like:

df =
id1, id2, string
1,   [2], 'foo'
2,   [3], 'bar'
3,   [4], 'baz'

I'd like to replace id2 with the value from the array.

Here is what I've tried:

x = df['id2'].map(lambda x : x[0])

With the hopes of x being a series with the values I want that I can column bind to my DF. What actually happens is that it errors out with an IndexError. In that case I made a function to apply, in place of the lambda function, so that it could except the error, but this returned all nulls.

Seems like this should be straight forward, but I haven't been able to figure it out even after sleeping on it.

1
  • 1
    What is the data type of id2 -- df.id2.dtype? Is it a string, list, np.array? Commented Sep 23, 2015 at 17:32

1 Answer 1

1

This works for if 'id2' is stored as array. You may just need to call tolist() to get the output you desire.

x = df['id2'].map(lambda x: x[0]).tolist()

Alternatively if 'id2' is stored as a string, you can use the ast package to call literal_eval to consume as an array:

import pandas as pd
from io import StringIO

data = StringIO(u'''id1,id2,string
1,[2],foo
2,[3],bar
3,[4],baz''')

df = pd.read_csv(data)

import ast
x = df['id2'].map(lambda x: ast.literal_eval(x)[0]).tolist()
Sign up to request clarification or add additional context in comments.

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.