1

I am trying to figure how to load my csv rows into a nested array.

For e.g., my csv file:

id a1 a2 b1 b2 c1 c2 d1 d2
1 1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15 16
3 17 18 19 20 21 22 23 24
...

How do I make it into an array like this:

for each row I want to group every two columns into what I am showing below:

[ 
  [[1, 2], [3, 4], [5, 6], [7, 8]],          #row 1
  [[9, 10], [11, 12], [13, 14], [15, 16]],   #row 2
  [[17, 18], [19, 20], [21, 22], [23, 24]],  #row 3
  ...
]

2 Answers 2

1

For nested list for all columns in pairs without id column use:

df = df.drop('id', axis=1)

L = np.reshape(df.to_numpy(), (len(df.index),len(df.columns) // 2,2)).tolist()
print (L)
[[[1, 2], [3, 4], [5, 6], [7, 8]],
 [[9, 10], [11, 12], [13, 14], [15, 16]],
 [[17, 18], [19, 20], [21, 22], [23, 24]]]
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, sorry but what if I don't have an id column? I was just using it to illustrate the number of rows
@Gbeck - then remove df = df.drop('id', axis=1)
can you explain this line L = np.reshape(df.to_numpy(), (len(df.index),len(df.columns) // 2,2)).tolist()?
@Gbeck - Sure, len(df.index) number of rows, len(df.columns) // 2 number of rows divide 2 and 2 for groups lenght
0

Group the dataframe by index i.e. level=0 (can be avoided if no duplicate entries in dataframe for any index), then for each group, filter the dataframe to get the pair of columns, then apply list on axis=1 for each filtered dataframe, finally concatenate all of these on axis=1, then apply list again on axis=1, finally call tolist():

(df.groupby(level=0)
.apply(lambda x:pd.concat([x.filter(like=c)
                          .apply(list, axis=1) for c in 'abcd'], axis=1)
       )
.apply(list, axis=1)
.tolist()
 )

OUTPUT:

[
    [
        [1, 2], [3, 4], [5, 6], [7, 8]
    ], 
    [
        [9, 10], [11, 12], [13, 14], [15, 16]
    ], 
    [
        [17, 18], [19, 20], [21, 22], [23, 24]
    ]
]

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.