Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

deleting list element if the element is present on another column of a dataframe

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading