I have the following scenario.
import pandas as pd
d = {'col1': [1, 2, 3], 'col2': [['apple'], [], ['romaine', 'potatoes']}
df = pd.DataFrame(data=d)
So the dataframe is:
col1 col2
0 1 [apple]
1 2 []
2 3 [romaine, potatoes]
I also have a dictionary:
my_dict = {"apple" : "fruit", "potatoes" : "vegetable", "romaine" : "lettuce"}
I want to create another column "col3" which will have a list of values from my_dict above:
col1 col2 col3
0 1 [apple] [fruit]
1 2 [] []
2 3 [romaine, potatoes] [lettuce, vegetable]
I want to write a single line of code using apply, map, lambda to achieve this:
df["col3"] = df.col2.apply(map(lambda x: pass if not x else condition_dict[x]))
I am really stuck and wonder if it is possible without writing a separate function and then passing as an argument to apply.