list of columns in common in two pandas dataframes

后端 未结 1 1844
旧巷少年郎
旧巷少年郎 2020-12-06 11:35

I\'m considering merge operations on dataframes each with a large number of columns. Don\'t want the result to have two columns with the same name. Am trying to view a list

相关标签:
1条回答
  • 2020-12-06 11:58

    Use numpy.intersect1d or intersection:

    a = np.intersect1d(df2.columns, df1.columns)
    print (a)
    ['B' 'C']
    
    a = df2.columns.intersection(df1.columns)
    print (a)
    Index(['B', 'C'], dtype='object')
    

    Alternative syntax for the latter option:

    df1.columns & df2.columns
    
    0 讨论(0)
提交回复
热议问题