Ten Minutes to Pandas
Concat
DF = PD. DataFrame (Np.random.randn (10, 4))Print(DF)#Break it into piecespieces = [Df[:3], Df[3:7], df[7:]]Print(Pd.concat (pieces))#0 1 2 3#0 0.879526-1.417311-1.309299 0.287933#1-1.194092 1.237536-0.375177-0.622846#2 1.449524 1.732103 1.866323 0.327194#3-0.028595 1.047751 0.629286-0.611354#4-1.237406 0.878287 1.407587-1.637072#5 0.536248 1.172208 0.405543 0.245162#6 0.166374 1.185840 0.132388-0.832135#7 0.750722-1.188307 1.306327 1.564907#8-0.755132-1.538270-0.173119 1.341313#9-0.572171 1.808220 0.688190-0.672612#0 1 2 3#0 0.879526-1.417311-1.309299 0.287933#1-1.194092 1.237536-0.375177-0.622846#2 1.449524 1.732103 1.866323 0.327194#3-0.028595 1.047751 0.629286-0.611354#4-1.237406 0.878287 1.407587-1.637072#5 0.536248 1.172208 0.405543 0.245162#6 0.166374 1.185840 0.132388-0.832135#7 0.750722-1.188307 1.306327 1.564907#8-0.755132-1.538270-0.173119 1.341313#9-0.572171 1.808220 0.688190-0.672612
Join
Joins in SQL like (Union table)
left = PD. DataFrame ({'Key': ['Foo','Foo'],'lval': [1, 2]}) right= PD. DataFrame ({'Key': ['Foo','Foo'],'Rval': [4, 5]})Print(left)Print(right)Print(Pd.merge (left, right, on='Key'))#Key Lval#0 Foo 1#1 Foo 2#Key Rval#0 Foo 4#1 Foo 5#Key Lval Rval#0 Foo 1 4#1 Foo 1 5#2 Foo 2 4#3 Foo 2 5
Merge
DF = PD. DataFrame (NP.RANDOM.RANDN (8, 4), columns=['A','B','C','D'])Print(DF) s= Df.iloc[3]Print(s) df.append (s, Ignore_index=True)Print(DF)#A B C D#0-1.744799-0.745689-0.066827-0.993191#1 0.843984 0.902578 0.845040 1.336861#2 0.865214 1.151313 0.277192-0.711557#3 0.917065-0.948935 0.110977 0.047466#4-1.309586 0.539592 1.956684-0.117199#5-0.431144 0.884499-0.828626-0.506894#6-1.263993-0.826366 1.426688-0.434647#7-0.567870-0.086037 2.166162-0.396294# /#A 0.917065#B-0.948935#C 0.110977#D 0.047466#Name:3, Dtype:float64# /#A B C D#0-1.744799-0.745689-0.066827-0.993191#1 0.843984 0.902578 0.845040 1.336861#2 0.865214 1.151313 0.277192-0.711557#3 0.917065-0.948935 0.110977 0.047466#4-1.309586 0.539592 1.956684-0.117199#5-0.431144 0.884499-0.828626-0.506894#6-1.263993-0.826366 1.426688-0.434647#7-0.567870-0.086037 2.166162-0.396294
Python Notes #17 # Pandas:merge