有没有办法像str.contains一样使用字典?
我有一本字典,它看起来像这样:
A = {'Hey':1}
我知道对于字典,它会查找完全匹配(区分大小写和空格),因此我不知道是否可以执行此操作。
我的数据框看起来像这样:
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?
我想要做的是使用我的字典并基本查看语句中的每一行,如果字符串包含Hey将其更改为1,此外,如果我可以这样做,我想知道是否可以在其中放置多个语句字典?像这样:
A = {'嘿','你好','嗨' :1}
我想要做的是在字典中放入一堆可能的字符串,如果在语句中找到这些字符串,则根据需要进行更改。在这个例子中,Hey是语句中唯一可以改变的单词。
我的预期结果如下:
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?
答案 0 :(得分:3)
我认为您可以先创建列表词典,然后将keys
与values
交换为d
和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?
答案 1 :(得分:1)
有更好的方法可以做到这一点,但想在进入会议之前发帖:)
首先,逻辑将如下工作 1)循环遍历列表中的每个元素 2)拆分列表中的句子 3)循环句子中的每个单词并检查数学 4)如果匹配则更新索引
假设a是字典
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'
答案 2 :(得分:0)
执行此操作的一点点方法,但允许您在一行中检查您想要的任意数量的单词。
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)