0

I have 3 huge dataframes that have different length of values

Ex,

A         B         C         
2981     2952     1287
2759     2295     2952
1284     2235     1284
1295     1928     0887
2295     1284     1966
         1567     1928
         1287     2374
                  2846
                  2578

I want to find the common values between the three columns like this

A         B         C     Common          
2981     2952     1287     1284
2759     2295     2952     2295
1284     2235     1284
1295     1928     0887
2295     1284     1966
         1567     2295
         1287     2374
                  2846
                  2578

I tried (from here)

df1['Common'] = np.intersect1d(df1.A, np.intersect1d(df2.B, df3.C))             

but I get this error, ValueError: Length of values does not match length of index

kiyas
  • 145
  • 10

1 Answers1

2

Idea is create Series with index filtered by indexing with length of array:

a = np.intersect1d(df1.A, np.intersect1d(df2.B, df3.C))
df1['Common'] = pd.Series(a, index=df1.index[:len(a)])

If same DataFrame:

a = np.intersect1d(df1.A, np.intersect1d(df1.B, df1.C))
df1['Common'] = pd.Series(a, index=df1.index[:len(a)])
print (df1)
        A       B     C  Common
0  2981.0  2952.0  1287  1284.0
1  2759.0  2295.0  2952  2295.0
2  1284.0  2235.0  1284     NaN
3  1295.0  1928.0   887     NaN
4  2295.0  1284.0  1966     NaN
5     NaN  1567.0  2295     NaN
6     NaN  1287.0  2374     NaN
7     NaN     NaN  2846     NaN
8     NaN     NaN  2578     NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252