merging and splitting of arrays in numpy and pandas
Merging
in NumPy
In NumPy, you can combine two arrays on both the vertical and horizontal axes by concatenate, specifying parameters axis=0 or Axis=1.
Import NumPy as NP import pandas as PD Arr1=np.ones (3,5) arr1 out[5]: Array ([[1., 1., 1., 1., 1.], [1., 1
., 1., 1., 1.], [1., 1., 1., 1., 1.]] Arr2=np.random.randn. Reshape (Arr1.shape) arr2 out[8]: Array ([[-0.09666833, 1.47064828,-1.94608976, 0.2651279,-0 .32894787], [1.01187699, 0.39171167, 1.49607091, 0.79216196, 0.33246644], [1.71266238, 0.86650837, 0 .77830394, -0.90519422, 1.55410056]]) np.concatenate ([arr1,arr2],axis=0) #在纵轴上合并 out[9]: Array ([[1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [1. , 1. , 1. , 1. , 1. ], [0.09666833, 1.47064828,-1.94608976, 0.2651279,-0.32894787], [1.01187699, 0.39171167, 1.49607091 , 0.79216196, 0.33246644], [1.71266238, 0.86650837, 0.77830394, -0.90519422, 1.55410056]] np.concatenate ([AR
R1,arr2],axis=1) #在横轴上合并OUT[10]: Array ([[1]. , 1. , 1. ,...,-1.94608976, 0.2651279,-0.32894787], [1. , 1. , 1. ,..., 1.49607091, 0.79216196, 0.33246644], [1. , 1. , 1. , ..., 0.77830394, -0.90519422, 1.55410056]] Np.hstack ([ARR1,ARR2]) # horizontal Horizon Np.vstack ([arr1,arr2]) # Vertical
Vertical
in Pandas
In pandas, merge is implemented by concat method, specifying parameters axis=0 or Axis=1, merging two arrays on the longitudinal and horizontal axes. Unlike NumPy, here are two dataframe to be placed in a list, that is, [frame1,frame2]
From pandas import dataframe
frame1=dataframe ([[1,2,3],[4,5,6]])
frame2=dataframe ([[[7,8,9],[10,11,12]])
the Pd.concat ([frame1,frame2],ignore_index=true) # merged array is a list of iterations.
out[25]:
0 1 2
0 1 2 3 1 4 5 6 0 7-8 9
1
pd.concat ([frame1,frame2],axis=1,ignore_index=true)
out[27]:
0 1 2 3 , 4 5 0 1 2 3 7 8 9 1 4 5 6 12
Split
By default, the NumPy array is created in row order of precedence . In terms of space, this means that for a two-dimensional number, the data items in each row are stored in adjacent positions within. Another order is column precedence.
For historical reasons, row and column precedence are also referred to as C and Fortran order respectively. In NumPy, row precedence and column precedence can be achieved through keyword Parameters order= ' C ' and order= ' F '.
arr=np.arange. Reshape (3,-1) arr out[29]: Array ([[0, 1, 2, 3, 4], [5, 6, 7, 8
, 9], [Ten, One, Arr.ravel]]) the #按照列优先, flattened. OUT[30]: Array ([0, 5,, ..., 4, 9,,.) Arr.ravel () out[31]: Array ([0, 1, 2, ...) Arr.reshape ((5,3)
, order= ' F ') # Fortran order Out[32]: Array ([[0, 11, 8], [5, 2, 13], [10, 7, 4], [1, 12, 9],
[6, 3, 14]]) Arr.reshape ((5,3), order= ' C ') out[33]: Array ([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] , [A, A]])