I have a NumPy Nd-array and the shape of the array is (3, 3, 2). I want to calculate the mean and sd of the array over each set/layer and want to save them in a pandas dataframe. I can do this using the following code
import pandas as pd
import numpy as np
data_array = np.ndarray(shape=(2,3,2))
final_result = pd.DataFrame(
{
"Mean": np.array(data_array).mean(),
"Mean_sd": np.array(data_array).mean(axis=0).std(ddof=1),
"Mean_1": np.array(data_array[0]).mean(),
"Mean_1_sd": np.array(data_array[0]).mean(axis=0).std(ddof=1),
"Mean_2": np.array(data_array[1]).mean(),
"Mean_2_sd": np.array(data_array[1]).mean(axis=0).std(ddof=1),
},
index=[0],
)
In the given example I have only 2 layers/sets. So, I took the layer/set number (i.e., [0] or [1]) manually to calculate the mean and sd.
"Mean_1": np.array(data_array[0]).mean(),
"Mean_2": np.array(data_array[1]).mean(),
But the real data_array is big (say, the shape of the array is (100, 3, 2)). So it is not possible (and eventually not the pythonic way) to take the layer/set numbers manually.
Is there any way to make it dynamic instead of taking layers/sets numbers manually and saving them in the pandas dataframe?