Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

python - Checking DataFrame column value for match in list

I have the following list:

list = ['abc', 'ace', 'aei']

And I have DataFrame:

df = pd.DataFrame({"A": ['abc','abd','abe','acd','ace','aci'], "B": ""})

I want write a function that will, for each value in df['A'], check for any match in the list. If a match exists, add "match" for that value in df['B']. So far the for loops I've tried don't seem to be working. For example:

for el in df['A']:
    if el in list:
        df['B'] = "match"
    else:
        pass

This populates every row of df['B'] with "match". Other efforts have not populated df['B'] when there is a match.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import pandas as pd

lst = ['abc', 'ace', 'aei']

dct = {"A": ['abc','abd','abe','acd','ace','aci'], "B": ""}

if len(list(set(lst) & set(dct['A']))) != 0:
    dct['B'] = 'match'


print (dct)

Output:

{'A': ['abc', 'abd', 'abe', 'acd', 'ace', 'aci'], 'B': 'match'}

If you still wish to use pandas:

import numpy as np
import pandas as pd

lst = ['abc', 'ace', 'aei']

dct = {"A": ['abc','abd','abe','acd','ace','aci'], "B": ""}

df['B'] = np.where(df['A'].isin(lst), 'match', '')

For partial matches:

lst = ['abc']

dct = {"A": ['abcdef','abd','abe','acd','ace','aci'], "B": ""}

if len([v for v in lst if v in " ".join(dct['A'])]) != 0:
    dct['B'] = 'match'

Output:

{'A': ['abcdef', 'abd', 'abe', 'acd', 'ace', 'aci'], 'B': 'match'}

OP request:

lst = ['abc', 'ace', 'aei']

dct = {"A": ['abcdef','aei','abe','acd','ace','aci'], "B": []}

for test in lst:
    for val in dct['A']:
        if test in val:
            dct['B'].append('match')
        else:
            dct['B'].append('nomatch')

print (dct)

Output:

{'A': ['abcdef', 'aei', 'abe', 'acd', 'ace', 'aci'], 'B': ['match', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'nomatch', 'match', 'nomatch', 'nomatch', 'match', 'nomatch', 'nomatch', 'nomatch', 'nomatch']}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...