In [2]:
import pandas as pd
import numpy as np
from pandas import DataFrame, Series
df = DataFrame(
    {'integer':[1,2,3,6,7,23,8,3],
     'float':[2,3.4,5,6,2,4.7,4,8],
     'string':['saya',None,'aku','cinta','kamu','a','b','jika']}
)
df
Out[2]:
float integer string
0 2.0 1 saya
1 3.4 2 None
2 5.0 3 aku
3 6.0 6 cinta
4 2.0 7 kamu
5 4.7 23 a
6 4.0 8 b
7 8.0 3 jika
In [5]:
df_new = DataFrame(
{'string':['a','b'],
 'str_value':[2,4]}
)
df_new
Out[5]:
str_value string
0 2 a
1 4 b
In [6]:
df_merge_inner = pd.merge(df,df_new,on='string',how='inner')
print df_merge_inner
   float  integer string  str_value
0    4.7       23      a          2
1    4.0        8      b          4
In [8]:
df_merge_outer = pd.merge(df,df_new,on='string',how='outer')
print df_merge_outer
   float  integer string  str_value
0    2.0        1   saya        NaN
1    3.4        2   None        NaN
2    5.0        3    aku        NaN
3    6.0        6  cinta        NaN
4    2.0        7   kamu        NaN
5    4.7       23      a          2
6    4.0        8      b          4
7    8.0        3   jika        NaN
In [9]:
df_merge_left = pd.merge(df,df_new,on='string',how='left')
df_merge_left
Out[9]:
float integer string str_value
0 2.0 1 saya NaN
1 3.4 2 None NaN
2 5.0 3 aku NaN
3 6.0 6 cinta NaN
4 2.0 7 kamu NaN
5 4.7 23 a 2
6 4.0 8 b 4
7 8.0 3 jika NaN
In [10]:
df_merge_right = pd.merge(df,df_new,on='string',how='right')
df_merge_right
Out[10]:
float integer string str_value
0 4.7 23 a 2
1 4.0 8 b 4