pandas return columns in dataframe that are not in other dataframe

后端 未结 4 1655
暖寄归人
暖寄归人 2020-12-20 03:22

I have two dataframes that look like this:

df_1 = pd.DataFrame({
\'A\' : [1.0, 2.0, 3.0, 4.0],
\'B\' : [100, 200, 300, 400],
\'C\' : [2, 3, 4, 5] 
                   


        
4条回答
  •  自闭症患者
    2020-12-20 03:44

    Numpy solution with numpy.setdiff1d:

    a = np.setdiff1d(df_2.columns, df_1.columns)
    print (a)
    ['D']
    

    Pandas solution with Index.difference:

    a = df_2.columns.difference(df_1.columns)
    print (a)
    Index(['D'], dtype='object')
    

    Another pandas methods are intersection, union and symmetric_difference :

    print (df_2.columns.intersection(df_1.columns))
    Index(['B', 'C'], dtype='object')
    
    print (df_2.columns.union(df_1.columns))
    Index(['A', 'B', 'C', 'D'], dtype='object')
    
    print (df_2.columns.symmetric_difference(df_1.columns))
    Index(['A', 'D'], dtype='object')
    

    And numpy functions are intersect1d, union1d and setxor1d:

    print (np.intersect1d(df_2.columns, df_1.columns))
    ['B' 'C']
    
    print (np.union1d(df_2.columns, df_1.columns))
    ['A' 'B' 'C' 'D']
    
    print (np.setxor1d(df_2.columns, df_1.columns))
    ['A' 'D']
    

提交回复
热议问题