Cannon
Cannon

Reputation: 319

Using dictionaries to change multiple keywords at once in one statement Python

Is there a way to use a dictionary like a str.contains?

I have a dictionary and it looks like this:

A = {'Hey':1}

I know that for dictionaries it looks for exact matches (case-sensitive and spaces) so I dont know if it is possible to do this.

my dataframe looks like this:

          Statements
0    Hey how are you?
1    Hey is their anyway to find that
2    Hey is their a way to prove this
3    over their, hey, how are you?

what I would like to do is use my dictionary and basically look through each line in Statements and if the string contains Hey change it to 1, furthermore, if I can do this I was wondering if I could put multiple statements inside the dictionary? like this:

A = {'Hey', 'Hello', 'Hi' : 1}

what I want to do is put a bunch of possible strings inside a dictionary and if those strings are found inside the statements then make the changes as necessary. In this example Hey is the only word present in statements that would get changed.

My expected results would be as follows:

          Statements
0    1 how are you?
1    1 is their anyway to find that
2    1 is their a way to prove this
3    over their, 1, how are you?

Upvotes: 0

Views: 75

Answers (3)

jezrael
jezrael

Reputation: 862611

I think you can create dict of lists first, then swap keys with values to d and replace:

L = ['Hey how are you?',
     'Hey is their anyway to find that',
     'Hey is their a way to prove this',
     'over their, hey, how are you?']

df = pd.DataFrame({'Statements':L})

A = {'1':['Hey', 'Hello', 'Hi', 'hey']}
d = {k: oldk for oldk, oldv in A.items() for k in oldv}
print (d)
{'Hi': '1', 'hey': '1', 'Hey': '1', 'Hello': '1'}

df['Statements'] = df['Statements'].replace(d, regex=True)
print (df)
                       Statements
0                  1 how are you?
1  1 is their anyway to find that
2  1 is their a way to prove this
3     over their, 1, how are you?

Upvotes: 3

Alex F
Alex F

Reputation: 2274

Little bit of a round-about way of doing it but allows you to check for any number of words you want in one line.

for i in range(df.shape[0]):
    line_split = df['Statements'][i].split()
    for j in range(len(line_split)):
        if line_split[j] in (['Hey', 'hey']):
           line_split[j] = '1'
    df['Statements'][i] = ' '.join(line_split)

Upvotes: 0

IVI
IVI

Reputation: 2116

There will be a better way of doing this but wanted to post before going into the meeting :)

Firstly the logic will work as follows 1) Loop through each element in the list 2) Split the sentence in the list 3) Loop through each word in sentence and check for mathces 4) If it matches then update the index

Assuming a is a dictionary

for line in a:
    sentence = line.split()
    for word in sentence:
        if word == 'Hey':
           # Found an index which contains the word so update the index here
           a[line] = 'New value for index'

Upvotes: 1

Related Questions