【数据分析可视化】 DataFrame的merge操作

2020-07-07 19:57:16 浏览数 (1)

代码语言:javascript复制
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
代码语言:javascript复制
# 通过字典,创建两个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

代码语言:javascript复制
df2 = DataFrame({'data2':[4,5,6],'key':['a','e','d']})
df2

data2

key

0

4

a

1

5

e

2

6

d

代码语言:javascript复制
# 前提两个数框(必须有相同的列名-key相同的情况)
pd.merge(df1,df2) 

data1

key

data2

0

1

a

4

1

4

a

4

代码语言:javascript复制
# merge参数on (两组数据靠哪一列merge)
pd.merge(df1,df2, on='key')

data1

key

data2

0

1

a

4

1

4

a

4

代码语言:javascript复制
# merge参数on 
pd.merge(df1,df2, on=None)

data1

key

data2

0

1

a

4

1

4

a

4

代码语言:javascript复制
# 参数how(如何去merge) how='inner' 共有
pd.merge(df1,df2, on='key', how='inner')

data1

key

data2

0

1

a

4

1

4

a

4

代码语言:javascript复制
# 左边为准
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

代码语言:javascript复制
# 右边为准
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

代码语言:javascript复制
# 左右边为准
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

0 人点赞