0

Suppose:

>>> df1 = pd.DataFrame({"A":[1,2,3],"B":[2,4,6]})
>>> df1
   A  B
0  1  2
1  2  4
2  3  6

and:

>>> df2 = pd.DataFrame({"A":[1,1,2,2,3,3, 4],"B":[2,3,4,5,6,7, 8], "C":[11,12,13,14,15,16, 17]})
>>> df2
   A  B   C
0  1  2  11
1  1  3  12
2  2  4  13
3  2  5  14
4  3  6  15
5  3  7  16
6  4  8  17

I expect to combine them and have the following where I have complete values of df2 for common rows on A:

   A  B   C
0  1  2  11
1  1  3  12
2  2  4  13
3  2  5  14
4  3  6  15
5  3  7  16

what I tried:

>>> df2.merge(df1, how='outer')
   A  B   C
0  1  2  11
1  1  3  12
2  2  4  13
3  2  5  14
4  3  6  15
5  3  7  16
6  4  8  17

Maybe I don't want merge,two dataframes are identical on columns, but one have more rows and more targets for some keys (keys are in A, and targets are B), I want all targets for keys on df1... I tried varies on and how options, none of them were my answer, unless you point it. Otherwise, the other question isn't helpful here

Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • 3
    what do you mean by *common rows*? `A,B = 3,7` does not occur in `df1`. It looks like your expected is `df2[df2['A'].isin(df1['A'])]`. – Quang Hoang May 24 '22 at 18:11
  • 1
    Maybe you want `df2[df2['A'].isin(df1['A'])]`? – Ynjxsjmh May 24 '22 at 18:12
  • @QuangHoang I editted I meant common rows on A – Ahmad May 24 '22 at 18:15
  • @Ynjxsjmh yes, I want that, does it works for multiple columns too? – Ahmad May 24 '22 at 18:16
  • @Ahmad *common* means merging with `how='inner'`, not `how='outer'`. And you should pass the merge key as well (and yes, `merge` works for multiple columns, no, `isin` does not) – Quang Hoang May 24 '22 at 18:16
  • @QuangHoang I want rows on df2 but limited to keys on df1 – Ahmad May 24 '22 at 18:17
  • Yes, that means `inner` merge. Please read the dup link. – Quang Hoang May 24 '22 at 18:18
  • @QuangHoang Maybe I don't want merge, I guess @Ynjx point works.... Two dataframe are identical on columns, but one have more rows and more targets for some keys (key is A, and targets are B), I want all targets for keys on A... I tried varies `on` and `how` options, none of them were my answer, unless you point it. Otherwise, the other question isn't helpful here – Ahmad May 24 '22 at 18:22
  • 2
    That's what I said initially, and you ask for multiple columns??? `df2.merge(df1[['A']], on=['A'], how='inner'])` is effectively the same with `isin` approach, but `isin` **does not** generalize to multiple column (keys). – Quang Hoang May 24 '22 at 18:24
  • @QuangHoang Sorry, if df1 also has distinct columns like `D` how can I keep both `C` (from df2) and `D` from df1? – Ahmad May 24 '22 at 18:35
  • 1
    `df2.merge(df1[['A','D']], on=['A'], how='inner')`. – Quang Hoang May 24 '22 at 18:49

0 Answers0