numpy - Loop over lists and concat in pd.dataframe -
i'm trying mulitply dataframe numpy array follows:
import numpy np pandas import* import pandas 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) i create loop 'concatenated automatic regardless of number of lists in c, avoiding writing each carray_i hand.
thanx
strongly suspect that
def concatarrays(a, b): pieces = [a*b in a] #(...) concatenated = concat(pieces, axis=1, join='outer') return concatenated will trick, depend on if pandas behaves expect in terms of iterating on data frames.
Comments
Post a Comment