0

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.

  • You could look into `apply` instead of `map`. – Shaido Jan 31 '22 at 04:14
  • 1
    Does this answer your question? [How can I use the apply() function for a single column?](https://stackoverflow.com/questions/34962104/how-can-i-use-the-apply-function-for-a-single-column) There are some answers here with good examples of how to add secondary inputs. – Shaido Jan 31 '22 at 04:16
  • you can try: `df['a'].map(lambda z: tie_to_dict(z, dictionary))` – monte Jan 31 '22 at 04:37

1 Answers1

1

Use:

temp = []
for i, row in df.iterrows()
   output = tie_to_dict(row['a'], dictionary)
   temp.append(output)
df['new_col'] = temp
keramat
  • 4,328
  • 6
  • 25
  • 38