我正在尝试将 DataFrame 与 numpy 数组相乘,如下所示:
import numpy as np
from pandas import*
import pandas as pd
C = np.arange(30).reshape(5, 6)
df = pd.DataFrame(np.arange(48).reshape((8, 6)), columns=list('abcdef'))
pxc = df / df.shift(1) - 1
def concatArrays(A, B):
cArrays_0 = np.sum(A[0,]*B, axis=1)
cArrays_1 = np.sum(A[1,]*B, axis=1)
cArrays_2 = np.sum(A[2,]*B, axis=1)
cArrays_3 = np.sum(A[3,]*B, axis=1)
cArrays_4 = np.sum(A[4,]*B, axis=1)
#(...)
pieces = [cArrays_0, cArrays_1, cArrays_2, cArrays_3, cArrays_4] #(...)
concatenated = concat(pieces, axis=1, join='outer')
return concatenated
print concatArrays(C, pxc)
我想创建一个循环,以便无论 C 中的列表数量如何,'concatenated 都是自动的,避免手动编写每个 cArray_i。
谢谢