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

Find the common columns between a list of Data frames

I want to find the common columns between a list of Data frames, the way that I started is, I defined x1 that is a lists of list ( for each data frames columns name), then I extract each sub list to a separate list.

I have the output as follows:

lst_1=['a1,a2,a3'] 

which has to be as follows, to be able to use set(lst_1) & set(lst_2)& etc :

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


lst_1=["a1","a2","a3"']

The code


x1=[]
dfs = [list of df]

for i, df in enumerate(dfs):
    x1.append([",".join((str(i) for i in df.columns))])

    globals()[f'lst_{i}']= x1[i]

>Solution :

reduce with set intersection

from functools import reduce

common = reduce(lambda a, b: set(a) & set(b), dfs)
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