Issue
I have this simplified DataFrame:
A | B |
---|---|
foo | A, B, C, D |
I create a dictionary d:
d = {'foo': 'A, B, C'}
Dictionary keys are in column A and their values are in column B. How can I remove any substrings that aren't part of my dictionary's values?
Desired DataFrame:
A | B |
---|---|
foo | A, B, C |
Solution
If need compare by spiltted values by ,
use:
d = {'foo': 'A, B, C'}
f = lambda x: ', '.join(y for y in x.B.split(', ') if y in x.A.split(', '))
df['B'] = df.assign(A = df['A'].map(d)).apply(f, axis=1)
print (df)
A B
0 foo A, B, C
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.