1
+ # View more python tutorials on my Youtube and Youku channel!!!
2
+
3
+ # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4
+ # Youku video tutorial: http://i.youku.com/pythontutorial
5
+
6
+ import pandas as pd
7
+ import numpy as np
8
+
9
+ # concatenating
10
+ # ingnore index
11
+ df1 = pd .DataFrame (np .ones ((3 ,4 ))* 0 , columns = ['a' ,'b' ,'c' ,'d' ])
12
+ df2 = pd .DataFrame (np .ones ((3 ,4 ))* 1 , columns = ['a' ,'b' ,'c' ,'d' ])
13
+ df3 = pd .DataFrame (np .ones ((3 ,4 ))* 2 , columns = ['a' ,'b' ,'c' ,'d' ])
14
+ res = pd .concat ([df1 , df2 , df3 ], axis = 0 , ignore_index = True )
15
+
16
+ # join, ('inner', 'outer')
17
+ df1 = pd .DataFrame (np .ones ((3 ,4 ))* 0 , columns = ['a' ,'b' ,'c' ,'d' ], index = [1 ,2 ,3 ])
18
+ df2 = pd .DataFrame (np .ones ((3 ,4 ))* 1 , columns = ['b' ,'c' ,'d' , 'e' ], index = [2 ,3 ,4 ])
19
+ res = pd .concat ([df1 , df2 ], axis = 1 , join = 'outer' ))
20
+ res = pd .concat ([df1 , df2 ], axis = 1 , join = 'inner' )
21
+
22
+ # join_axes
23
+ res = pd .concat ([df1 , df2 ], axis = 1 , join_axes = [df1 .index ])
24
+
25
+ # append
26
+ df1 = pd .DataFrame (np .ones ((3 ,4 ))* 0 , columns = ['a' ,'b' ,'c' ,'d' ])
27
+ df2 = pd .DataFrame (np .ones ((3 ,4 ))* 1 , columns = ['a' ,'b' ,'c' ,'d' ])
28
+ df2 = pd .DataFrame (np .ones ((3 ,4 ))* 1 , columns = ['b' ,'c' ,'d' , 'e' ], index = [2 ,3 ,4 ])
29
+ res = df1 .append (df2 , ignore_index = True )
30
+ res = df1 .append ([df2 , df3 ])
31
+
32
+ s1 = pd .Series ([1 ,2 ,3 ,4 ], index = ['a' ,'b' ,'c' ,'d' ]))
33
+ res = df1 .append (s1 , ignore_index = True )
34
+
35
+ print (res )
0 commit comments