Python 矩阵拼接

情况1:空余位置自动补齐(类似MATLAB矩阵拼接)

import pandas as pd
import numpy as np

a = 1
b = [4, 5, 0]
c = pd.concat([pd.DataFrame([a]), pd.DataFrame([b])], axis=0) # axis=0按行拼接,类似MATLAB矩阵[a;b];axis=1按列拼接,类似MATLAB矩阵[a,b]
d = np.array(c)

情况2:矩阵累拼接(初始矩阵为空矩阵)

方法1:利用pandas
该方法不需要保证列数相同

import numpy as np
import pandas as pd

temp = []
for i in range(100):
    pre = np.random.randn(5,10)
    temp = pd.concat([pd.DataFrame(temp), pd.DataFrame(pre)], axis=0)

方法2:利用numpy
该方法要保证列数相同

import numpy as np

temp = np.empty((0, 10))
for i in range(100):
    pre = np.random.randn(3,10)
    temp = np.vstack((temp, pre))

你可能感兴趣的:(Python 矩阵拼接)