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
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]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With