0

I have data in csv format of the following form:

A   B   a1  a2  a3  a4  a5
1   0   100 34  44  1   1
2   0   101 1   44  11  3
3   0   105 3   55  21  22
4   0   45  4   52  1   45
5   0   57  5   42  3   56
6   0   89  78  1   3   67
7   0   34  99  2   4   98
8   0   57  23  2   5   2

I would like to store the data from columns a1 on in one array to give:

100
34
44
1
1
101
1
44
.
.

I can do this using python by reading the columns I want from the csv into a pandas dataframe. Converting each column into an array, looping through each array, and appending the values to one new array. I know this is very inefficient. Is there a better way to do this? Thanks!

1 Answer 1

2

Use numpy.ravel with selecting columns by columns names:

A = df[['a2', 'a3', 'a4', 'a5']].values.ravel()

Or by filter:

A = df.filter(like='a').values.ravel()

Or by position - from second column to end:

A = df.iloc[:, 2:].values.ravel()

print (A)
[100  34  44   1   1 101   1  44  11   3 105   3  55  21  22  45   4  52
   1  45  57   5  42   3  56  89  78   1   3  67  34  99   2   4  98  57
  23   2   5   2]
Sign up to request clarification or add additional context in comments.

1 Comment

@Sjoseph - Glad can help! Small advice - For prevent downvotes add some code, error, what you try.

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.