1

I have the following dataframe:

   Pclass   Sex     Fare    Embarked    Title   Family
    3          0    1.0        0          0     1
    1          1    2.0        1          3     1
    3          1    1.0        1          2     0
    1          1    2.0        3          3     1
    3          0    2.0        1          1     0

I need to take each row and turn it into a 6X32 matrix.

(Example of 3X10)

3,3,3,3,3,3,3,3,3,3
0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1

I need that but for every element of the column and 32 times instead of 10.

My code:

from itertools import repeat
array = []
for i in range(0,len(datos)):
    rows = datos.iloc[i,:]
    for j in range(0,len(rows)):
        array.append([rows[j]]*10)

I'm not sure how to do it to separate each row into a unique array.

5
  • At least let us know what you have already tried. Don't just expect someone to do it for you. Commented Aug 16, 2017 at 16:21
  • 1
    You're absolutely right! Sorry forgot to submit my idea. Commented Aug 16, 2017 at 16:29
  • try to reshape your array after repeat() Commented Aug 16, 2017 at 16:31
  • You say 7x32... but I only see 6 columns. Is that a typo? Commented Aug 16, 2017 at 16:32
  • It is, I'm sorry. Commented Aug 16, 2017 at 16:33

2 Answers 2

2

Everything at once

np.dstack([df.values.astype(int)] * 32)

You can even turn it into a series with matching index

pd.Series(np.dstack([df.values.astype(int)] * 32).tolist(), df.index).apply(np.array)

0    [[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,...
1    [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
2    [[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,...
3    [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
4    [[3, 3, 3, 3, 3, 
Sign up to request clarification or add additional context in comments.

Comments

1

for the first row:

a = df.iloc[0].values.repeat(10).reshape(6,10)


#out: array([[ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.],
#   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
#   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
#   [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

now for the whole df:

for i in range(df.shape[1] -1 ):
    a = df.iloc[i].values.repeat(10).reshape(6,10)
    # ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.