turn numpy array pf shape (128, 24, 91) to pd dataframe in python in python

To convert a numpy array of shape (128, 24, 91) to a pandas dataframe in Python, you can use the pd.DataFrame() function from the pandas library. Here's an example:

main.py
import numpy as np
import pandas as pd

# Creating a numpy array with shape (128, 24, 91)
numpy_array = np.random.random((128, 24, 91))

# Reshaping the numpy array to a 2D shape (128*24, 91)
reshaped_array = numpy_array.reshape((128*24, 91))

# Converting the reshaped array to a pandas dataframe
df = pd.DataFrame(reshaped_array)

# Optional: Setting column names for the dataframe
df.columns = ['Column1', 'Column2', 'Column3', ...]

# Optional: Setting row names/index for the dataframe
df.index = ['Row1', 'Row2', 'Row3', ...]

# Printing the dataframe
print(df)
568 chars
21 lines

In this example, we first reshape the numpy array to a 2D shape with dimensions (128*24, 91) using the reshape() function. Then, we can create a pandas dataframe using pd.DataFrame() by passing the reshaped array as an argument. Optionally, you can set column names and row names/index for the dataframe using the .columns and .index attributes respectively.

Note: The example assumes you have numpy and pandas libraries installed.

related categories

gistlibby LogSnag