[SOLVED] How to access index inside function for applymap in pandas?

Issue

I am using a custom function in pandas that iterates over cells in a dataframe, finds the same row in a different dataframe, extracts it as a tuple, extracts a random value from that tuple, and then adds a user specified amount of noise to the value and returns it to the original dataframe. I was hoping to find a way to do this that uses applymap, is it possible? I couldn’t find a way using applymap, so I used itertuples, but an applymap solution should be more efficient.

import pandas as pd
# Mock data creation
key = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4,5,6], 'col3':[7,8,9]})
results = pd.DataFrame(np.zeros((3,3)))

def apply_value(value):    
    key_index = # <-- THIS IS WHERE I NEED A WAY TO ACCESS INDEX
    key_tup = key.iloc[key_index]
    length = (len(key_tup) - 1)
    random_int = random.randint(1, length)
    random_value = key_tup[random_int]
    return random_value

results = results.applymap(apply_value)

Solution

If I understood your problem correctly, this piece of code should work. The problem is that applymap does not hold the index of the dataframe, so what you have to do is to apply nested apply functions: the first iterates over rows, and we get the key from there, and the second iterates over columns in each row. Hope it helps. Let me know if it does 😀

# Mock data creation
key = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4,5,6], 'col3':[7,8,9]})
results = pd.DataFrame(np.zeros((3,3)))
def apply_value(value, key_index):
    key_tup= key.loc[key_index]
    length = (len(key_tup) - 1)
    random_int = random.randint(1, length)
    random_value = key_tup[random_int]
    return random_value
results = results.apply(lambda x: x.apply(lambda d: apply_value(d, x.name)), axis=1) 

Answered By – ivallesp

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *