从“for 循环”生成的 numpy 数组动态地将数据存储在 pandas 数据框的列中

Dynamically store data in the columns of pandas dataframe from numpy arrays being generated from “for loop”

in[31]: day_1_variable.shape
out[31]: (241, 241)

这是包含 241 * 241(行 * 列)的 10 个 numpy 数组的字典

df_dictionary = {'arrays_to_iterate': {'day_1': day_1_variable,
                                   'day_2': day_2_variable, 
                                   'day_3': day_3_variable,
                                   .
                                   .
                                   .
                                   .
                                   .
                                   .

                                   'day_10': day_10_variable}}
day = 10
for days in np.arange(1,day+1):
    numpy_array_to_iterate = df_dictionary ['arrays_to_iterate']['day_'+str(days)+'_rf']
       
    variable_value_array=np.zeros((0),dtype='float')          ## empty array of variable value created
    for i in np.arange(numpy_array_to_iterate.shape[0]):      ## iterating array rows 
        for j in np.arange(numpy_array_to_iterate.shape[1]):  ## iterating array column  
            variable_value_at_specific_point=numpy_array_to_iterate[i][j]

            variable_value_array=np.append(variable_value_array,variable_value_at_specific_point) ## values filled in array


    df_xyz = pd.DataFrame()
    for i in np.arange(1,day+1):
        col_name = 'variable_day_' + str(i)
        df_xyz.loc[:, col_name] = variable_value_array
df_xyz

我想将每一天的数组数据存储在 pandas 数据框的列中,该数据框具有每个相应日期的变量值

但是我得到的输出在每一列中都有最后一天的值

          variable_day_1    variable_day_2  ........... variable_day_10
0         0.0625            0.0625          ........... 0.0625  
1         0.0625            0.0625          ........... 0.0625  
2         0.0625            0.0625          ........... 0.0625  
3         0.0625            0.0625          ........... 0.0625  
4         0.0625            0.0625          ........... 0.0625  
... ... ... ... ... ... ... ... ... ... ...

58076     0.0000            0.0000          ........... 0.0000  
58077     0.0000            0.0000          ........... 0.0000  
58078     0.0000            0.0000          ........... 0.0000  
58079     0.0000            0.0000          ........... 0.0000  
58080     0.0000            0.0000          ........... 0.0000  
58081 rows × 10 columns

如何操作?

使用 Numpy stack over the dictionary values (this will give you a Numpy array with shape (10, 241, 241)) then use reshape 将形状修改为 (10,58081),然后转置,将日期作为列放置。接下来,转换为 Pandas 数据框并使用字典键修复列名。

import pandas as pd
import numpy as np

#setup
np.random.seed(12345)
df_dictionary = {}
days = {f'day_{d}': np.random.rand(241,241).round(2) for d in range(1,11)}
df_dictionary['arrays_to_iterate'] = days
print(df_dictionary)

#code
all_days = np.stack(list(df_dictionary['arrays_to_iterate'].values())).reshape(10, -1).T
df = pd.DataFrame(all_days)
df.columns = df_dictionary['arrays_to_iterate'].keys()

print(df)

来自 df_dictionary

的输出
{'arrays_to_iterate':
    {'day_1':
        array(
        [[0.93, 0.32, 0.18, ..., 0.62, 0.89, 0.78],
        [0.72, 0.31, 0.36, ..., 0.5 , 0.89, 0.38],
        ...,
        [0.36, 0.62, 0.77, ..., 0.03, 0.57, 0.04],
        [0.02, 0.07, 0.66, ..., 0.62, 0.5 , 0.04]]),
    'day_2': array(
        [[0.14, 0.13, 0.91, ..., 0.06, 0.72, 0.93],
        [0.13, 0.02, 0.09, ..., 0.39, 0.72, 0.13],
    ...

来自 df

的输出
       day_1  day_2  day_3  day_4  day_5  day_6  day_7  day_8  day_9  day_10
0       0.93   0.14   0.06   0.10   0.01   0.66   0.67   0.18   0.93    0.40
1       0.32   0.13   0.81   0.57   0.23   0.60   0.48   0.07   0.08    0.32
2       0.18   0.91   0.95   0.27   0.36   0.11   0.25   0.71   0.24    0.44
3       0.20   0.51   0.52   0.62   0.09   0.31   0.19   0.78   0.83    0.58
4       0.57   0.14   0.89   0.51   0.67   0.29   0.48   0.95   0.36    0.97
...      ...    ...    ...    ...    ...    ...    ...    ...    ...     ...
58076   0.98   0.20   0.54   0.96   0.89   0.24   0.05   0.81   0.35    0.57
58077   0.53   0.96   0.04   0.60   0.16   0.38   0.83   0.49   0.28    0.02
58078   0.62   0.50   0.74   0.67   0.43   0.30   0.91   0.68   0.15    0.43
58079   0.50   0.11   0.57   0.42   0.85   0.97   0.86   0.60   0.75    0.33
58080   0.04   0.74   0.74   0.94   0.98   0.35   0.52   0.12   0.47    0.53

[58081 rows x 10 columns]