17-numpy笔记-莫烦pandas-5

代码

import pandas as pd
import numpy as np

left=pd.DataFrame({'key':['K0','K1','K2','K3'],
                    'A':['A0','A1','A3','A3'],
                    'B':['B0','B1','B2','B3'],})

right=pd.DataFrame({'key':['K0','K1','K2','K3'],
                    'C':['C0','C1','C3','C3'],
                    'D':['D0','D1','D2','D3'],})

print('-1-')
print(left)
print(right)

res = pd.merge(left,right,on='key')
print(res)


left=pd.DataFrame({'key1':['K0','K0','K1','K2'],
                    'key2':['K0','K1','K0','K1'],
                    'A':['A0','A1','A3','A3'],
                    'B':['B0','B1','B2','B3'],})

right=pd.DataFrame({'key1':['K0','K1','K1','K2'],
                    'key2':['K0','K0','K0','K0'],
                    'C':['C0','C1','C3','C3'],
                    'D':['D0','D1','D2','D3'],})


print('-2-')
res = pd.merge(left,right,on=['key1','key2'])
print(left)
print(right)
print(res)

# default
print('-3-')
res = pd.merge(left,right,on=['key1','key2'],how='inner')
print(left)
print(right)
print(res)

print('-4-')
res = pd.merge(left,right,on=['key1','key2'],how='outer')
print(left)
print(right)
print(res)

print('-5-')
res = pd.merge(left,right,on=['key1','key2'],how='right')
print(left)
print(right)
print(res)

print('-6-')
res = pd.merge(left,right,on=['key1','key2'],how='left')
print(left)
print(right)
print(res)

print('-7-')
df1 = pd.DataFrame({'col1':[0,1],'col_left':['a','b']})
df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]})
print(df1)
print(df2)
res = pd.merge(df1,df2,on='col1',how='outer',indicator=True)
print(res)

res = pd.merge(df1,df2,on='col1',how='outer',indicator=True)
print(res)

res = pd.merge(df1,df2,on='col1',how='outer',indicator='indicator_column')
print(res)


df1 = pd.DataFrame({'A':['A0','A1','A2'],
                    'B':['B0','B1','B2']},
                    index=['K0','K1','K2'])

df2 = pd.DataFrame({'C':['C0','C1','C2'],
                    'D':['D0','D1','D2']},
                    index=['K0','K1','K2'])

print(df1)
print(df2)

print('-8-')
res=pd.merge(left,right,left_index=True,right_index=True,how='outer')
print(res)

print('-9-')
res=pd.merge(left,right,left_index=True,right_index=True,how='inner')
print(res)

boys = pd.DataFrame({'k':['K0','K1','K2'],'age':[1,2,3]})
girls = pd.DataFrame({'k':['K0','K0','K3'],'age':[4,5,6]})

print('-10-')
print(boys)
print(girls)

res = pd.merge(boys, girls, on='k', suffixes=['_boy','_girl'],how='inner')
print(res)

res = pd.merge(boys, girls, on='k', suffixes=['_boy','_girl'],how='outer')
print(res)

  

 

输出

-1-
  key   A   B
0  K0  A0  B0
1  K1  A1  B1
2  K2  A3  B2
3  K3  A3  B3
  key   C   D
0  K0  C0  D0
1  K1  C1  D1
2  K2  C3  D2
3  K3  C3  D3
  key   A   B   C   D
0  K0  A0  B0  C0  D0
1  K1  A1  B1  C1  D1
2  K2  A3  B2  C3  D2
3  K3  A3  B3  C3  D3
-2-
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A3  B2
3   K2   K1  A3  B3
  key1 key2   C   D
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C3  D2
3   K2   K0  C3  D3
  key1 key2   A   B   C   D
0   K0   K0  A0  B0  C0  D0
1   K1   K0  A3  B2  C1  D1
2   K1   K0  A3  B2  C3  D2
-3-
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A3  B2
3   K2   K1  A3  B3
  key1 key2   C   D
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C3  D2
3   K2   K0  C3  D3
  key1 key2   A   B   C   D
0   K0   K0  A0  B0  C0  D0
1   K1   K0  A3  B2  C1  D1
2   K1   K0  A3  B2  C3  D2
-4-
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A3  B2
3   K2   K1  A3  B3
  key1 key2   C   D
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C3  D2
3   K2   K0  C3  D3
  key1 key2    A    B    C    D
0   K0   K0   A0   B0   C0   D0
1   K0   K1   A1   B1  NaN  NaN
2   K1   K0   A3   B2   C1   D1
3   K1   K0   A3   B2   C3   D2
4   K2   K1   A3   B3  NaN  NaN
5   K2   K0  NaN  NaN   C3   D3
-5-
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A3  B2
3   K2   K1  A3  B3
  key1 key2   C   D
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C3  D2
3   K2   K0  C3  D3
  key1 key2    A    B   C   D
0   K0   K0   A0   B0  C0  D0
1   K1   K0   A3   B2  C1  D1
2   K1   K0   A3   B2  C3  D2
3   K2   K0  NaN  NaN  C3  D3
-6-
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A3  B2
3   K2   K1  A3  B3
  key1 key2   C   D
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C3  D2
3   K2   K0  C3  D3
  key1 key2   A   B    C    D
0   K0   K0  A0  B0   C0   D0
1   K0   K1  A1  B1  NaN  NaN
2   K1   K0  A3  B2   C1   D1
3   K1   K0  A3  B2   C3   D2
4   K2   K1  A3  B3  NaN  NaN
-7-
   col1 col_left
0     0        a
1     1        b
   col1  col_right
0     1          2
1     2          2
2     2          2
   col1 col_left  col_right      _merge
0     0        a        NaN   left_only
1     1        b        2.0        both
2     2      NaN        2.0  right_only
3     2      NaN        2.0  right_only
   col1 col_left  col_right      _merge
0     0        a        NaN   left_only
1     1        b        2.0        both
2     2      NaN        2.0  right_only
3     2      NaN        2.0  right_only
   col1 col_left  col_right indicator_column
0     0        a        NaN        left_only
1     1        b        2.0             both
2     2      NaN        2.0       right_only
3     2      NaN        2.0       right_only
     A   B
K0  A0  B0
K1  A1  B1
K2  A2  B2
     C   D
K0  C0  D0
K1  C1  D1
K2  C2  D2
-8-
  key1_x key2_x   A   B key1_y key2_y   C   D
0     K0     K0  A0  B0     K0     K0  C0  D0
1     K0     K1  A1  B1     K1     K0  C1  D1
2     K1     K0  A3  B2     K1     K0  C3  D2
3     K2     K1  A3  B3     K2     K0  C3  D3
-9-
  key1_x key2_x   A   B key1_y key2_y   C   D
0     K0     K0  A0  B0     K0     K0  C0  D0
1     K0     K1  A1  B1     K1     K0  C1  D1
2     K1     K0  A3  B2     K1     K0  C3  D2
3     K2     K1  A3  B3     K2     K0  C3  D3
-10-
    k  age
0  K0    1
1  K1    2
2  K2    3
    k  age
0  K0    4
1  K0    5
2  K3    6
    k  age_boy  age_girl
0  K0        1         4
1  K0        1         5
    k  age_boy  age_girl
0  K0      1.0       4.0
1  K0      1.0       5.0
2  K1      2.0       NaN
3  K2      3.0       NaN
4  K3      NaN       6.0

  

 

posted @ 2018-09-07 22:17  路边的十元钱硬币  阅读(253)  评论(0编辑  收藏  举报