BilzR
BilzR

Reputation: 79

Extract certain elements based on element location from another column

I have two columns in a DataFrame, crewname is a list of crew members worked on a film. Director_loc is the location within the list of the director.

I want to create a new column which has the name of the director.

    crewname                                           Director_loc

    [John Lasseter, Joss Whedon, Andrew Stanton, J...  0
    [Larry J. Franco, Jonathan Hensleigh, James Ho...  3
    [Howard Deutch, Mark Steven Johnson, Mark Stev...  0
    [Forest Whitaker, Ronald Bass, Ronald Bass, Ez...  0
    [Alan Silvestri, Elliot Davis, Nancy Meyers, N...  5
    [Michael Mann, Michael Mann, Art Linson, Micha...  0
    [Sydney Pollack, Barbara Benedek, Sydney Polla...  0
    [David Loughery, Stephen Sommers, Peter Hewitt...  2
    [Peter Hyams, Karen Elise Baldwin, Gene Quinta...  0
    [Martin Campbell, Ian Fleming, Jeffrey Caine, ...  0

I've tried a number of codes using list comprehension, enumerate etc. I'm a bit embarrassed to put them here.

Any help will be appreciated.

Upvotes: 1

Views: 60

Answers (1)

jezrael
jezrael

Reputation: 863196

Use indexing with list comprehension:

df['name'] = [a[b] for a , b in zip(df['crewname'], df['Director_loc'])]
print (df)
                                         crewname  Director_loc  \
0  [John Lasseter,  Joss Whedon,  Andrew Stanton]             2   
1          [Larry J. Franco,  Jonathan Hensleigh]             1   

                  name  
0       Andrew Stanton  
1   Jonathan Hensleigh  

Upvotes: 2

Related Questions