Skip to content Skip to sidebar Skip to footer

How To Get Intersection Of Dataframes Based On Column Labels?

I'd like to get the intersection of pandas dataframes df_a und df_b based on column labels. Consider df_a import pandas as pd df_a = pd.DataFrame( columns=[0.1, 0.2, 0.6],

Solution 1:

Get the intersection of the two column lists? Then get the value from one of the dataframes:

Is this what you need?

>>>col_intersect = list(set(df_a.columns.tolist()).intersection(df_b.columns.tolist()))>>>col_intersect
[0.1, 0.6]

>>>new_df = df_a[col_intersect]>>>new_df
   0.1  0.6
0   59   50

Solution 2:

A quick solution would be to find the common columns and then do a merge inner operation:

import numpy as np
import pandas as pd


df_a = pd.DataFrame(
    columns=[0.1, 0.2, 0.6],
    data=[[59, 10, 50],[1,2,3]],
)
df_b = pd.DataFrame(
    columns=[0.1, 0.4, 0.6],
    data=[[59, 20, 50],[4,5,6]],
)

#get an array of common column names
col = np.intersect1d(df_a.columns.tolist(), df_b.columns.tolist())

df_all = pd.merge(df_a[col], df_b[col], how='inner')

Post a Comment for "How To Get Intersection Of Dataframes Based On Column Labels?"