1

How do I convert a 3-dimensional NumPy array [sample_index, channel, data] into a dataframe with each sample as a row and the channels as a column?

The numpy arrays look like this:

import numpy as np
np.random.uniform(-25, 100, size=(6400,3,100*100))
2
  • You've described what you want to happen with each sample/channel. What would you like to do with the 3rd dimension? Commented Oct 23, 2020 at 15:55
  • @CameronRiddell Those 100*100 data points can be stored in a list I guess. Commented Oct 23, 2020 at 16:07

1 Answer 1

1

Let's try:

samples, size = 6, 2*2

data = np.arange(samples*size *3).reshape(samples, 3, size)

pd.DataFrame(data.swapaxes(1,2).reshape(-1,3),
             np.repeat(np.arange(samples), size)
            )

Output, here, each index represents a sample:

    0   1   2
0   0   4   8
0   1   5   9
0   2   6  10
0   3   7  11
1  12  16  20
1  13  17  21
1  14  18  22
1  15  19  23
2  24  28  32
2  25  29  33
2  26  30  34
2  27  31  35
3  36  40  44
3  37  41  45
3  38  42  46
3  39  43  47
4  48  52  56
4  49  53  57
4  50  54  58
4  51  55  59
5  60  64  68
5  61  65  69
5  62  66  70
5  63  67  71
Sign up to request clarification or add additional context in comments.

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.