samedi 2 juillet 2016

How to apply a function to each row for transforming into a data frame and then append them in Python

I am new to Python. I have a question on transforming and appending.For example I have a df,each row contains 24 values, you could treat it as each element in a 6*4 matrix. and then I would extract the neighborhood value of each cell and I only need those sum of the neighborhood distance equals to 9. This is the example code I made:

{submatrix = []
 for i in range(0,4):
    for j in range(0,2):
        submatrix.append(df.iloc[3].reshape(6,4)[i:i+3,j:j+3].flatten())
  submatrix= np.array(submatrix)
  location = ("loc22","loc23","loc32","loc33","loc42","loc43","loc52","loc53")
  location = np.array(location)
  neighboor = np.c_[location,submatrix]
  neighboor = pd.DataFrame(neighboor)
  print neighboor
}

which would lead to a 8* 10 data frame but how to apply it into each row and append them? This is obviously not enough

  {
  transform = df.apply(lambda x: transform(x),axis=1)
  }

what I expected is more like each row produce a 8*10 data frame and append it. for example, for a two row original data frame, it would produce a 16*10 data frame since each row would produce 8*10 dataframe

Aucun commentaire:

Enregistrer un commentaire