Shang Lu
Shang Lu

Reputation: 91

Use values of a dictionary to label a column with keys of dicrtionary in Python

data = {'Name':['Tom', 'nick', 'krish', 'jack'],
        'Note':['The color is red', 'This is a white blouse', 'I love the blue hoodie', 'What is that orange box?']}
  
# Create DataFrame
df = pd.DataFrame(data)

This is my dataset DF. I want to find some color words in column "Note", so I created a dictionary that all the values are the color words I want to search. Then I want to create a new column that returns the dictionary keys.

F={'One':'red','Two':'white', 'Three':'blue', 'Four':'orange'}

I want to do that in a for loop, but it's not working. It seems the y argument stops being replaced after being assigned with values. Could someone advice? Thanks!

for i in range(4):
    print(list(F.values())[i])
    df['C']=np.where(df['Note'].str.contains(list(F.values())[i]), list(F.keys())[i], 
                          )

Upvotes: 1

Views: 575

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

If I understand correctly you need:

df['Match'] = df['Note'].replace({f'.*{v}.*': k for k, v in F.items()}, regex=True)

And now:

>>> df
    Name                      Note  Match
0    Tom          The color is red    One
1   nick    This is a white blouse    Two
2  krish    I love the blue hoodie  Three
3   jack  What is that orange box?   Four
>>> 

Or if you want to only replace the words:

df['Match'] = df['Note'].replace({v: k for k, v in F.items()}, regex=True)

Output:

>>> df
    Name                      Note                    Match
0    Tom          The color is red         The color is One
1   nick    This is a white blouse     This is a Two blouse
2  krish    I love the blue hoodie  I love the Three hoodie
3   jack  What is that orange box?   What is that Four box?
>>>

Upvotes: 1

Related Questions