【数据分析可视化】 DataFrame的merge操作
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
# 通过字典,创建两个DataFrame
df1 = DataFrame({'data1':[1,2,3,4],'key':['a','b','c','a']})
df1
|
data1 |
key |
---|
0 |
1 |
a |
1 |
2 |
b |
2 |
3 |
c |
3 |
4 |
a |
df2 = DataFrame({'data2':[4,5,6],'key':['a','e','d']})
df2
|
data2 |
key |
---|
0 |
4 |
a |
1 |
5 |
e |
2 |
6 |
d |
# 前提两个数框(必须有相同的列名-key相同的情况)
pd.merge(df1,df2)
|
data1 |
key |
data2 |
---|
0 |
1 |
a |
4 |
1 |
4 |
a |
4 |
# merge参数on (两组数据靠哪一列merge)
pd.merge(df1,df2, on='key')
|
data1 |
key |
data2 |
---|
0 |
1 |
a |
4 |
1 |
4 |
a |
4 |
# merge参数on
pd.merge(df1,df2, on=None)
|
data1 |
key |
data2 |
---|
0 |
1 |
a |
4 |
1 |
4 |
a |
4 |
# 参数how(如何去merge) how='inner' 共有
pd.merge(df1,df2, on='key', how='inner')
|
data1 |
key |
data2 |
---|
0 |
1 |
a |
4 |
1 |
4 |
a |
4 |
# 左边为准
pd.merge(df1,df2, on='key', how='left')
|
data1 |
key |
data2 |
---|
0 |
1 |
a |
4.0 |
1 |
2 |
b |
NaN |
2 |
3 |
c |
NaN |
3 |
4 |
a |
4.0 |
# 右边为准
pd.merge(df1,df2, on='key', how='right')
|
data1 |
key |
data2 |
---|
0 |
1.0 |
a |
4 |
1 |
4.0 |
a |
4 |
2 |
NaN |
e |
5 |
3 |
NaN |
d |
6 |
# 左右边为准
pd.merge(df1,df2, on='key', how='outer')
|
data1 |
key |
data2 |
---|
0 |
1.0 |
a |
4.0 |
1 |
4.0 |
a |
4.0 |
2 |
2.0 |
b |
NaN |
3 |
3.0 |
c |
NaN |
4 |
NaN |
e |
5.0 |
5 |
NaN |
d |
6.0 |
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~