Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two pandas dataframes many-to-one

How do I merge the following datasets:

df = A
date abc
1    a
1    b
1    c
2    d
2    dd
3    ee
3    df

df = B
date ZZZ
1    a
2    b
3    c

I want to get smth like this:

date abc  ZZZ
1    a     a
1    b     a
1    c     a
2    d     b
2    dd    b
3    ee    c
3    df    c

I tried this code:

aa = pd.merge(A, B, left_on="date", right_on="date", how="left", validate="m:1")

But I have the following mistake:

TypeError: merge() got an unexpected keyword argument 'validate'

I update my pandas using (conda update pandas), but still get the same error

Please, advise me this issue.

like image 851
Alberto Alvarez Avatar asked Oct 21 '25 15:10

Alberto Alvarez


2 Answers

According to df.merge docs validate was added in version 0.21.0. You are using an older version so you should update the version of pandas you are using.

like image 108
DeepSpace Avatar answered Oct 23 '25 04:10

DeepSpace


As @DeepSpace mentioned, you may need to upgrade your pandas.

To replicate the check in earlier versions, you can do something like this:

import pandas as pd

df1 = pd.DataFrame(index=['a', 'a', 'b', 'b', 'c'])
df2 = pd.DataFrame(index=['a', 'b', 'c'])

x = [i for i in df2.index if i in set(df1.index)]
len(x) == len(set(x))  # True


df1 = pd.DataFrame(index=['a', 'a', 'b', 'b', 'c'])
df2 = pd.DataFrame(index=['a', 'b', 'c', 'a'])

y = [i for i in df2.index if i in set(df1.index)]
len(y) == len(set(y))  # False
like image 34
jpp Avatar answered Oct 23 '25 03:10

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!