import pandas as pd
data1 = [[2000,'size56'],[2001,'size56'],[2002,'size09']]
columns1=['a','b']
df1 = pd.DataFrame(data=data1,columns=columns1)
print(df1)
data2 = [['3000','size56'],['3001','size56'],['3002','size56'],['3003','size09']]
columns2=['a','b']
df2 = pd.DataFrame(data=data2, columns=columns2)
df1['g'] = df1.groupby(['b']).cumcount()
df2['g'] = df2.groupby(['b']).cumcount()
df = pd.merge(df1, df2, on=['b', 'g'] , how='inner')
print(df)
df1['g'] = df1.groupby(['b']).cumcount() df2['g'] = df2.groupby(['b']).cumcount()
df = pd.merge(df1, df2, on=['b', 'g'] , how='inner') print(df)
import pandas as pd
data1 = [[2000,'size56'],[2001,'size56'],[2002,'size09']]
columns1=['a','b']
df1 = pd.DataFrame(data=data1,columns=columns1)
print(df1)
data2 = [['3000','size56'],['3001','size56'],['3002','size56'],['3003','size09']]
columns2=['A','B']
df2 = pd.DataFrame(data=data2, columns=columns2)
I want to merge the following two df1 and df2 in pandas (python)
df1:
a b
0 2000 size56
1 2001 size56
2 2002 size09
df2:
A B
0 3000 size56
1 3001 size56
2 3002 size56
3 3003 size09
I hope to get this result:
df3:
a b A B
0 2000 size56 3000 size56
1 2001 size56 3001 size56
2 2002 size09 3003 size09
Any suggestion is highly appreciated!
Jason Cai
please use the following code to get the answer. Thanks a lot!
df1['g'] = df1.groupby(['b']).cumcount() df2['g'] = df2.groupby(['b']).cumcount()