pandas: get the exact corresponding value with the corresponding index based on a value in another column

I have a column of string(sentence) and a column of comma separated list of strings as follows:

df = pd.DataFrame({ 'text':['the weather is nice though', 'How are you today','the beautiful girl and the nice boy'],
'pos':[['DET', 'NOUN', 'VERB','ADJ', 'ADV'],['QUA', 'VERB', 'PRON', 'ADV'], ['DET', 'ADJ', 'NOUN','CON','DET', 'ADJ', 'NOUN' ]]})

and I would like to somehow compare the columns, and create a third column where if the ‘pos’ column contains the value ‘ADJ’, I would find its corresponding value in the ‘text’ column (in this case in the first row I have ‘nice’) and return its index as well in a form of a dictionary . so this is how the third column should look like;


1 {'nice' : 3}
2 {}
3 {'beautiful':1, 'nice':6}

so far I have tried the following:

df['Third_column']= ' '
df['liststring'] = [' '.join(map(str, l)) for l in df['pos']]

but do not know how to proceed to get the exact word and the index


What you describe is exactly what pandas.DataFrame.apply does.

If you want to calculate another column/row according to other columns/rows in pandas, this method should be considered.

import pandas as pd

def extract_words(row):
    word_pos = {}
    text_splited = row.text.split()
    for i, p in enumerate(row.pos):
        if p == 'ADJ':
            word_pos[text_splited[i]] = i
    return word_pos

df = ...
df['Third_column'] = df.apply(extract_words, axis=1)