每天学点python数据分析软件:4-4 DataFrame的merge操作




每天学点python数据分析软件:4-4 DataFrame的merge操作

jupyter notebook代码

spyder代码如下(请运行体验输出的结果)

import pandas as pd
import numpy as np
from pandas import Series,DataFrame

df = DataFrame({'key':['x','y','z','x'],'data1':[1,2,3,4]})

print(df)

df1 = DataFrame({'key':['x','a','b','c'],'data2':[6,2,3,5]})

print(df1)

df2 =pd.merge(df,df1)

print(df2)

df3=pd.merge(df,df1,on='key',how='inner')

print(df3)

print(pd.merge(df,df1,on='key',how='outer'))

print(pd.merge(df,df1,on='key',how='left'))

print(pd.merge(df,df1,on='key',how='right'))