Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Use index value in apply function over a column

I was wondering if there's a way to use index value while using apply over a column of the dataframe. Suppose I have df like:

  col1  col2
0    a    [0,1,2]
1    b    [0,2]
2    c    [0,1,2]

I want to write an apply function on df.col2 such that it removes the index values from the list in col2 leaving a df like:

  col1  col2
0    a    [1,2]
1    b    [0,2]
2    c    [0,1]

The index value may or may not be in the list. But if it does exist in the list it should be removed. Note that this isn't the actual use-case but similar to what I need. I have

df.col2.apply(lambda x: f(x))

and in the f(x) I want to be able to access the index value of x if its possible or a workaround. I know that df.apply() can work on the column values and df.index.map() can work on the index. Is there a method in Pandas that combines the use-cases of the two in one single elegant solution. Thanks for the help.

UPDATE: the index is an integer value and it will be constrained in such a way that its consecutive whole numbers. The col2 will have a list for each index. I want to check if the index is in that list and remove it from the list if it exists. So lets say for row index 3 we have list [27,36,3,9,7]. I want to drop 3 from the list. The list is unordered

like image 830
Fizi Avatar asked Aug 25 '16 18:08

Fizi


Video Answer


1 Answers

If I understand your question correctly, this should do the job:

df.apply(lambda x: x.name in x.col2 and x.col2.remove(x.name), axis=1)

With the example from the original post:

In [226]: df
Out[226]: 
  col1       col2
0    a  [0, 1, 2]
1    b     [0, 2]
2    c  [0, 1, 2]

In [227]: df.apply(lambda x: x.name in x.col2 and x.col2.remove(x.name), axis=1);

In [228]: df
Out[228]: 
  col1    col2
0    a  [1, 2]
1    b  [0, 2]
2    c  [0, 1]
like image 75
fuglede Avatar answered Oct 02 '22 15:10

fuglede