Home > Net >  Filter Pandas dataframe based on combination of keywords
Filter Pandas dataframe based on combination of keywords

Time:12-06

Supposed I have as dataframe as,

Role A Role B
0 Project Viewer Project Manager
1 Project Viewer Project Developer
2 Project Viewer Project Approver
3 Project Manager Project Developer
4 Project Manager Project Approver
5 Project Developer Project Approver

And I have these combinations of keywords/filter words

Combo 1 Combo 2
0 approve create
1 authorize develop
2 permit manage

So I want an output whereby the dataframe at the top gets filtered and only combinations that contains these keywords are left. Something like this:

Role A Role B
0 Project Manager Project Approver
1 Project Developer Project Approver

If someone has the solution please help

CodePudding user response:

Let me share you a sample, you can use it based on your needs;

import pandas as pd
 
combo_1 = [...]
combo_2 = [...]
data_frame = pd.DataFrame(...)
print(data_frame[dataframe["Role A"].isin(combo_1) & dataframe["Role B"].isin(combo_2)]) 

CodePudding user response:

May be reading https://pandas.pydata.org/docs/user_guide/merging.html will help. To filter things out you can (ab)use an "inner" join on the first with the third pd.

However, what you want with the second pd is not clear to me.

  • Related