>>> frame2
addr age name
0 beijing 12 zhang
1 shanghai 24 li
2 hangzhou 24 cao
>>> frame1
addr name
0 beijing zhang
1 shanghai li
2 hangzhou cao
3 shenzhen han
>>> pd.merge(frame1,frame2) 以name列为连接进行拼接
addr name age
0 beijing zhang 12
1 shanghai li 24
2 hangzhou cao 24
>>> pd.merge(frame1,frame2,on='name') 指定 列 和拼接方式
addr_x name addr_y age
0 beijing zhang beijing 12
1 shanghai li shanghai 24
2 hangzhou cao hangzhou 24
>>> pd.merge(frame1,frame2,on='name',how='outer')
addr_x name addr_y age
0 beijing zhang beijing 12.0
1 shanghai li shanghai 24.0
2 hangzhou cao hangzhou 24.0
3 shenzhen han NaN NaN
>>> pd.merge(frame1,frame2,on='name',how='inner')
addr_x name addr_y age
0 beijing zhang beijing 12
1 shanghai li shanghai 24
2 hangzhou cao hangzhou 24
>>> pd.merge(frame1,frame2,on='name',how='left')
addr_x name addr_y age
0 beijing zhang beijing 12.0
1 shanghai li shanghai 24.0
2 hangzhou cao hangzhou 24.0
3 shenzhen han NaN NaN
>>> pd.merge(frame1,frame2,on='name',how='right')
addr_x name addr_y age
0 beijing zhang beijing 12
1 shanghai li shanghai 24
2 hangzhou cao hangzhou 24
>>> pd.merge(frame1,frame2,on='name',left_index=True)
addr_x name addr_y age
0 beijing zhang beijing 12
1 shanghai li shanghai 24
2 hangzhou cao hangzhou 24
>>> pd.merge(frame1,frame2,on='name',right_index=True)
addr_x name addr_y age
0 beijing zhang beijing 12
1 shanghai li shanghai 24
2 hangzhou cao hangzhou 24
>>> pd.merge(frame1,frame2,on='addr',right_index=True)
addr name_x age name_y
0 beijing zhang 12 zhang
1 shanghai li 24 li
2 hangzhou cao 24 cao
>>> frame1.columns=['addr1','name1']
>>> frame1.join(frame2)
addr1 name1 addr age name 修改掉重复的列名称,然后join()
0 beijing zhang beijing 12.0 zhang
1 shanghai li shanghai 24.0 li
2 hangzhou cao hangzhou 24.0 cao
3 shenzhen han NaN NaN NaN
>>> array1
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> array1=np.arange(9).reshape((3,3))+6
>>> array2=np.arange(9).reshape((3,3))
>>> array1
array([[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
>>> np.concatenate([array1,array2],axis=1) np模块中对元组进行concatenate()
array([[ 6, 7, 8, 0, 1, 2],
[ 9, 10, 11, 3, 4, 5],
[12, 13, 14, 6, 7, 8]])
>>> np.concatenate([array1,array2],axis=0)
array([[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]])
>>>
>>> np.concatenate([array1,array2])
array([[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]])
>>> ser1=pd.Series(np.random.rand(4)) pd模块中也有concat()
>>> ser1
0 0.998915
1 0.117503
2 0.747180
3 0.641508
dtype: float64
>>> ser1=pd.Series(np.random.rand(4)*100)
>>> ser1
0 8.818592
1 42.317816
2 43.274021
3 23.245148
dtype: float64
>>> ser2=pd.Series(np.random.rand(4)*100,index=[5,6,7,8])
>>> ser2
5 58.416554
6 11.840838
7 38.146851
8 0.135517
dtype: float64
>>> pd.concat([ser1,ser2])
0 8.818592
1 42.317816
2 43.274021
3 23.245148
5 58.416554
6 11.840838
7 38.146851
8 0.135517
dtype: float64
>>> pd.concat([ser1,ser2],axis=1)
0 1
0 8.818592 NaN
1 42.317816 NaN
2 43.274021 NaN
3 23.245148 NaN
5 NaN 58.416554
6 NaN 11.840838
7 NaN 38.146851
8 NaN 0.135517
>> pd.concat([ser1,ser2],axis=1,keys=[1,2])
1 2
0 8.818592 NaN
1 42.317816 79.632793
2 43.274021 96.700070
3 23.245148 64.573269
4 NaN 68.629709
>>> ser2.index=[2,4,5,6]
>>> ser2
2 79.632793
4 96.700070
5 64.573269
6 68.629709
dtype: float64
>>> ser1.combine_first(ser2) 对缺额的数据进行填充 combin_first()
0 8.818592
1 42.317816
2 43.274021
3 23.245148
4 96.700070
5 64.573269
6 68.629709
dtype: float64
>>> ser1
0 a
1 b
2 c
3 d
dtype: object
>>> ser2
2 0
4 1
5 2
6 3
dtype: int32
>>> ser2.combine_first(ser1) ser1在后
0 a
1 b
2 0
3 d
4 1
5 2
6 3
dtype: object
>>> ser1[:2].combine_first(ser2) ser1在前
0 a
1 b
2 0
4 1
5 2
6 3
dtype: object
>>> frame1=pd.DataFrame({'name':['zhang','li','wang'],'age':[12,45,34],'addr':['beijing','shanghai','shenzhen']})
>>> frame1
addr age name
0 beijing 12 zhang
1 shanghai 45 li
2 shenzhen 34 wang
>>> frame1.stack() frame的进栈和出栈
0 addr beijing
age 12
name zhang
1 addr shanghai
age 45
name li
2 addr shenzhen
age 34
name wang
dtype: object
>>> frame1.stack().unstack()
addr age name
0 beijing 12 zhang
1 shanghai 45 li
2 shenzhen 34 wang
>>> frame1.stack().unstack(0) 列和索引转换
0 1 2
addr beijing shanghai shenzhen
age 12 45 34
name zhang li wang
>>> longframe=pd.DataFrame({'color':['white','white','white','red','red','red','black','black','black'],'item':['ball','pen','mug','ball','pen','mug','ball','pen','mug'],'value':np.random.rand(9)})
>>> longframe
color item value 对冗余的消除,将longframe转换为wideframe
0 white ball 0.260358
1 white pen 0.543955
2 white mug 0.456874
3 red ball 0.967021
4 red pen 0.657271
5 red mug 0.984256
6 black ball 0.550236
7 black pen 0.731625
8 black mug 0.006728
>>> wideframe=longframe.pivot('color','item')
>>> wideframe
value
item ball mug pen
color
black 0.550236 0.006728 0.731625
red 0.967021 0.984256 0.657271
white 0.260358 0.456874 0.543955
>>> frame1
addr age name
0 beijing 12 zhang
1 shanghai 12 li
2 beijing 12 wang
>>> del frame['addr']
Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
del frame['addr']
NameError: name 'frame' is not defined
>>> del frame1['addr']
>>> frame1
age name
0 12 zhang
1 12 li
2 12 wang