Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - pandas left join - why more results?

How is it possible that a pandas left join like

df.merge(df2, left_on='first', right_on='second', how='left')

increases the data frame from 221309 to 1388680 rows?

edit

shape of df 1 (221309, 83)

shape of df2 (7602, 6)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As @JonClements has already said in the comment it's a result of duplicated entries in the columns used for merging/joining. Here is a small demo:

In [5]: df
Out[5]:
   a   b
0  1  11
1  1  12
2  2  21

In [6]: df2
Out[6]:
   a    c
0  1  111
1  1  112
2  2  221
3  2  222
4  3  311

In [7]: df.merge(df2, on='a', how='left')
Out[7]:
   a   b    c
0  1  11  111
1  1  11  112
2  1  12  111
3  1  12  112
4  2  21  221
5  2  21  222

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...