1

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],
    data=[[59, 10, 50]],
)
df_a
    0.1     0.2     0.6
0   59      10      50

and df_b

df_b = pd.DataFrame(
    columns=intervals_b,
    data=[[59, 20, 50]],
)
    0.1     0.4     0.6
0   59      20      50

. How do I get the expected intersection?

    0.1     0.6
0   59      50
thinwybk
  • 4,193
  • 2
  • 40
  • 76

2 Answers2

0

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
Joe
  • 879
  • 2
  • 6
  • 15
0

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')
julian
  • 451
  • 2
  • 8