I have a list of dataframe list=[df1,df2,df3,df4,...df10]
the dataframe are constructed as following :
>df1
col1 col2 col3 col4
Y 2 XX PP
I have another dataframe DATA_SEL
such that
>DATA_SEL
col1 col2 col3 col4
A KK C D
A1 PP C D
...................
..................
If the first value (a string) of col4
for every dataframe in list
(df1 ,df2,df3....df10
)does not match with any value of col2
in DATA_SEL
, I want to delete that df
from list
.
How could I possibly do that?
Also if I want to construct a new list, list2
where the first value (a string) of col4
for every dataframe in list
(df1 ,df2,df3....df10
) matches with any value of col2
in DATA_SEL
, how to do that?
CodePudding user response:
Use list comprehension with filtering:
L = [df1,df2,df3,df4,...df10]
#tested first value of col4
out = [x for x in L if DATA_SEL['col2'].eq(x.at[x.index[0], 'col4']).any()]
#if first row has index == 0
out = [x for x in L if DATA_SEL['col2'].eq(x.at[0, 'col4']).any()]
#tested any value of col4
out = [x for x in L if DATA_SEL['col2'].isin(x['col4']).any()]