take the column data from two different excel files to construct a three dimensional array with a part of the data in the columns and the other in the rows in python

To achieve this, you can use the pandas library to read the excel files and manipulate the data to construct a 3D array.

First, you need to import pandas library:

main.py
import pandas as pd
20 chars
2 lines

Then, you can use the read_excel() function to read the data from the excel files. Let's assume that the column data from the first file is in column A and the second file is in column B. You can read both files as follows:

main.py
df1 = pd.read_excel('file1.xlsx', usecols=['A'])
df2 = pd.read_excel('file2.xlsx', usecols=['B'])
98 chars
3 lines

Next, you can use the numpy library to construct a 3D numpy array. Let's assume that you want to use the first 10 rows of data from file1 and file2 to construct the 3D array. You can do this as follows:

main.py
import numpy as np

data1 = df1.iloc[:10].values   # get first 10 rows of data from file1
data2 = df2.iloc[:10].values   # get first 10 rows of data from file2

# combine the data into a 3D array
arr = np.array([data1, data2])
227 chars
8 lines

This will create a 3D numpy array with data1 as the first slice and data2 as the second slice. You can access a specific slice of the array as follows:

main.py
slice1 = arr[0]   # get the first slice (data1)
slice2 = arr[1]   # get the second slice (data2)
97 chars
3 lines

You can then manipulate the data in the slices as needed.

gistlibby LogSnag