Pandas: Use index value in apply function over a column
<p>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:</p>

<pre><code>  col1  col2
0    a    [0,1,2]
1    b    [0,2]
2    c    [0,1,2]
</code></pre>

<p>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:</p>

<pre><code>  col1  col2
0    a    [1,2]
1    b    [0,2]
2    c    [0,1]
</code></pre>

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

<pre><code>df.col2.apply(lambda x: f(x))
</code></pre>

<p>and in the f(x) I want to be able to access the index value of x if its possible or a workaround. <strong>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</strong>. Thanks for the help.</p>

<p>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</p>

<pre><code>def name_drop(x):
    x_ = x.drop('col2')
    _x = pd.Series(x.col2)
    _x = _x[_x != x.name].tolist()
    x = x_.append(pd.Series({'col2': _x}))
    return x

df.apply(name_drop, axis=1)
</code></pre>

<p><a href="http://i.stack.imgur.com/Xw04j.png" rel="nofollow"><img src="http://i.stack.imgur.com/Xw04j.png" alt="enter image description here"></a></p>

python pandas dataframe apply