Aesler
Aesler

Reputation: 181

Creating new dataframe by iterating through list of array values

I would like to iterate over a large list, where each list index is a point in time in the time series. For each row of the list there is an 2-dimensional array with start and end values for 10 user ID's (0-9) which are the same for each array.

I would then like to create a new data frame such that I have the row index (for each moment in time) and then a column for each of the 10 user ID's such that they have their own time series. It has taken me far too long to figure out this data type, but here is the reproducible sample version:

list1 = np.transpose(np.array([[1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0],[1.2,2.3,3.4,4.6,5.5,6.6,7.1,8.2,9.0,10.7]]))
list2 = np.transpose(np.array([[1.4,2.6,3.4,4.9,6.5,5.6,8.7,1.8,4.9,10.8],[1.4,2.4,3.5,4.7,6.5,5.6,7.5,8.4,9.3,10.2]]))
list3 = np.transpose(np.array([[4.1,5.2,5.5,6.1,6.5,5.9,7.7,8.8,9.9,10.0],[1.1,2.2,3.3,4.8,5.5,5.7,7.7,8.6,9.0,10.0]]))

list_of_arrays = [list1,list2,list3]

The output would be as follows:

    start1 end1 start2 end2 start3 end3 ... (etc) ... start10  end10
0    1.1   1.2   2.2   2.3   3.3    3.4 ...            10.0    10.7
1    1.4   1.4   2.6   2.4   3.4    3.5 ...            10.8    10.2
2    4.1   1.1   5.2   2.2   5.5    3.3 ...            10.0    10.0
... (large n rows)

Upvotes: 0

Views: 48

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150745

Another option with ravel:

pd.DataFrame([l.ravel() for l in list_of_arrays])

Upvotes: 1

rafaelc
rafaelc

Reputation: 59274

Looks like a simple reshape

pd.DataFrame([arr.reshape(-1) for arr in list_of_arrays])

    0    1    2    3    4    5    6   ...   13   14   15   16   17    18    19
0  1.1  1.2  2.2  2.3  3.3  3.4  4.4  ...  7.1  8.8  8.2  9.9  9.0  10.0  10.7
1  1.4  1.4  2.6  2.4  3.4  3.5  4.9  ...  7.5  1.8  8.4  4.9  9.3  10.8  10.2
2  4.1  1.1  5.2  2.2  5.5  3.3  6.1  ...  7.7  8.8  8.6  9.9  9.0  10.0  10.0

Then you can just add the column names as needed.

Upvotes: 1

Related Questions