编程语言
首页 > 编程语言> > python数据分析——pandas的拼接操作

python数据分析——pandas的拼接操作

作者:互联网

pandas的拼接操作

pandas的拼接分为两种:

1. 使用pd.concat()级联

pandas使用pd.concat函数,与np.concatenate函数类似,只是多了一些参数:

objs
axis=0
keys
join='outer' / 'inner':表示的是级联的方式,outer会将所有的项进行级联(忽略匹配和不匹配),而inner只会将匹配的项级联到一起,不匹配的不级联
ignore_index=False

1)匹配级联

In [1]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
In [2]:
df1 = DataFrame(data=np.random.randint(0,100,size=(3,3)),index=['a','b','c'],columns=['A','B','C'])
df2 = DataFrame(data=np.random.randint(0,100,size=(3,3)),index=['a','d','c'],columns=['A','d','C'])
In [7]:
pd.concat((df1,df1),axis=0,join='inner')
Out[7]:
 ABC
a 59 40 89
b 71 5 76
c 29 34 87
a 59 40 89
b 71 5 76
c 29 34 87

2) 不匹配级联

不匹配指的是级联的维度的索引不一致。例如纵向级联时列索引不一致,横向级联时行索引不一致

有2种连接方式:

In [11]:
pd.concat((df1,df2),axis=1,join='outer')
Out[11]:
 ABCAdC
a 59.0 40.0 89.0 50.0 26.0 45.0
b 71.0 5.0 76.0 NaN NaN NaN
c 29.0 34.0 87.0 31.0 82.0 35.0
d NaN NaN NaN 23.0 95.0 94.0

3) 使用df.append()函数添加

由于在后面级联的使用非常普遍,因此有一个函数append专门用于在后面添加

2. 使用pd.merge()合并

merge与concat的区别在于,merge需要依据某一共同的列来进行合并

使用pd.merge()合并时,会自动根据两者相同column名称的那一列,作为key来进行合并。

注意每一列元素的顺序不要求一致

参数:

1) 一对一合并

In [12]:
df1 = DataFrame({'employee':['Bob','Jake','Lisa'],
                'group':['Accounting','Engineering','Engineering'],
                })
df1
Out[12]:
 employeegroup
0 Bob Accounting
1 Jake Engineering
2 Lisa Engineering
In [13]:
df2 = DataFrame({'employee':['Lisa','Bob','Jake'],
                'hire_date':[2004,2008,2012],
                })
df2
Out[13]:
 employeehire_date
0 Lisa 2004
1 Bob 2008
2 Jake 2012
In [14]:
pd.merge(df1,df2,how='outer')
Out[14]:
 employeegrouphire_date
0 Bob Accounting 2008
1 Jake Engineering 2012
2 Lisa Engineering 2004

2) 多对一合并

In [15]:
df3 = DataFrame({
    'employee':['Lisa','Jake'],
    'group':['Accounting','Engineering'],
    'hire_date':[2004,2016]})
df3
Out[15]:
 employeegrouphire_date
0 Lisa Accounting 2004
1 Jake Engineering 2016
In [16]:
df4 = DataFrame({'group':['Accounting','Engineering','Engineering'],
                       'supervisor':['Carly','Guido','Steve']
                })
df4
Out[16]:
 groupsupervisor
0 Accounting Carly
1 Engineering Guido
2 Engineering Steve
In [17]:
pd.merge(df3,df4)
Out[17]:
 employeegrouphire_datesupervisor
0 Lisa Accounting 2004 Carly
1 Jake Engineering 2016 Guido
2 Jake Engineering 2016 Steve

3) 多对多合并

In [18]:
df1 = DataFrame({'employee':['Bob','Jake','Lisa'],
                 'group':['Accounting','Engineering','Engineering']})
df1
Out[18]:
 employeegroup
0 Bob Accounting
1 Jake Engineering
2 Lisa Engineering
In [19]:
df5 = DataFrame({'group':['Engineering','Engineering','HR'],
                'supervisor':['Carly','Guido','Steve']
                })
df5
Out[19]:
 groupsupervisor
0 Engineering Carly
1 Engineering Guido
2 HR Steve
In [21]:
pd.merge(df1,df5,how='outer')
Out[21]:
 employeegroupsupervisor
0 Bob Accounting NaN
1 Jake Engineering Carly
2 Jake Engineering Guido
3 Lisa Engineering Carly
4 Lisa Engineering Guido
5 NaN HR Steve

4) key的规范化

In [10]:
df1 = DataFrame({'employee':['Jack',"Summer","Steve"],
                 'group':['Accounting','Finance','Marketing']})
In [11]:
df2 = DataFrame({'employee':['Jack','Bob',"Jake"],
                 'hire_date':[2003,2009,2012],
                'group':['Accounting','sell','ceo']})
In [22]:
display(df1,df2)
 
 employeegroup
0 Bob Accounting
1 Jake Engineering
2 Lisa Engineering
 
 employeehire_date
0 Lisa 2004
1 Bob 2008
2 Jake 2012
In [12]:
df1 = DataFrame({'employee':['Bobs','Linda','Bill'],
                'group':['Accounting','Product','Marketing'],
               'hire_date':[1998,2017,2018]})
In [13]:
df5 = DataFrame({'name':['Lisa','Bobs','Bill'],
                'hire_dates':[1998,2016,2007]})
In [23]:
display(df1,df5)
 
 employeegroup
0 Bob Accounting
1 Jake Engineering
2 Lisa Engineering
 
 groupsupervisor
0 Engineering Carly
1 Engineering Guido
2 HR Steve

5) 内合并与外合并:out取并集 inner取交集

In [25]:
df6 = DataFrame({'name':['Peter','Paul','Mary'],
               'food':['fish','beans','bread']}
               )
df7 = DataFrame({'name':['Mary','Joseph'],
                'drink':['wine','beer']})
In [26]:
display(df6,df7)
 
 namefood
0 Peter fish
1 Paul beans
2 Mary bread
 
 namedrink
0 Mary wine
1 Joseph beer
In [27]:
df6 = DataFrame({'name':['Peter','Paul','Mary'],
               'food':['fish','beans','bread']}
               )
df7 = DataFrame({'name':['Mary','Joseph'],
                'drink':['wine','beer']})
display(df6,df7)
pd.merge()
 
 namefood
0 Peter fish
1 Paul beans
2 Mary bread
 
 namedrink
0 Mary wine
1 Joseph beer

标签:数据分析,Jake,python,DataFrame,Engineering,pd,Accounting,Lisa,pandas
来源: https://www.cnblogs.com/bilx/p/11635867.html