Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to select a filter a list of columns within a selection list without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I want to be able to filter a DataFrame and keep the lines where a list of columns is in a selection list.
df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3,5], 'C': range(4)})
Input:
df A B C 0 5 1 0 1 6 2 1 2 3 3 2 3 4 5 3 filter_list = [(6,2),(3,3)]
Expected result:
df A B C 1 6 2 1 2 3 3 2
I have tried with loc and map, but I don’t manage to find a solution.
Thanks in advance.
Answer
A colleague gave me a faster solution on big DataFrames by creating a filtering DataFrame:
from pandas import merge, DataFrame df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3,5], 'C': range(4)}) filter_list = [(6,2),(3,3)] filter_df = DataFrame(filter_list, columns=['A','B']) filtr = merge(df, filter_df, on=["A","B"], how="inner")
We are here to answer your question about How to select a filter a list of columns within a selection list - If you find the proper solution, please don't forgot to share this with your team members.