Intersection of two or more DataFrame columns

后端 未结 3 627
盖世英雄少女心
盖世英雄少女心 2020-12-18 12:24

I am trying to find the intersect of three dataframes, however the pd.intersect1d does not like to use three dataframes.

import numpy as np
imp         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 12:43

    inclusive_list = np.intersect1d(np.intersect1d(df1.columns, df2.columns), df3.columns)
    

    Note that the arguments passed to np.intersect1d (https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.intersect1d.html) are expected to be two arrays (ar1 and ar2).

    Passing 3 arrays means that the assume_unique variable within the function is being set as an array (expected to be a bool).

    You can also use simple native python set methods if you don't want to use numpy

    inclusive_list = set(df1.columns).intersection(set(df2.columns)).intersection(set(df3.columns))
    

提交回复
热议问题