简体   繁体   中英

Extract element from Pandas dataframe column and create new column

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM