'Trying to apply complex functions to columns in pandas

I'm trying to apply/map a function which contains two inputs to a single column in a pandas df, in order to create a new column.

Based on this answer, I understand how to use .map to apply a function to one column.

However, my function I am using requires two inputs - the data from the dataframe itself, and a dictionary from a separate data source. Is there a way to do this, or should I rethink my entire strategy?

df = pd.DataFrame({'col1': ['a', 'b'], 'col2': ['c', 'd'], 'col3': ['e', 'f']}
df

Let's say I have the above dataframe, and my function is something like:

def tie_to_dict(data_from_df, dictionary):
# Does something with both inputs

Based on my understanding, I could do something like this if the tie_to_dict function only required 1 input (the value from the df itself).

df['new_column'] = df['a'].map(tie_to_dict)

But this doesn't work:

df['new_column'] = df['a'].map(tie_to_dict(df['a'], dictionary)

How would I resolve this? If it matters, the dictionary I will be using contains certain substrings with other properties. I want the function to return a value from the key,value pair in the dictionary of the key substring is in the data.



Solution 1:[1]

Use:

temp = []
for i, row in df.iterrows()
   output = tie_to_dict(row['a'], dictionary)
   temp.append(output)
df['new_col'] = temp

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1