#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series,DataFrame
df1=pd.DataFrame({'key':['b','b','a','a','b','a','c'],'data1':range(7)})
df2=pd.DataFrame({'key':['a','b','d'],'data2':range(3)})
print(df1)
print(df2)
merge12 = pd.merge(df1,df2,on='key',sort='key')
print(merge12)
merge12outer = pd.merge(df1,df2,on='key',how='outer',sort='key')
print(merge12outer)
# df3=pd.DataFrame({'group_val':[3.5,7]},index=['a','b'])
# print(df3)
# indexmerge = pd.merge(df2,df3,left_on='key',right_index=True,how='outer')
# print(indexmerge)
merge12join = df1.set_index('key').join(df2.set_index('key'))
print(merge12join
pandas contact join merge 操作
最新推荐文章于 2024-06-20 16:32:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)