将熊猫数据帧数组转换为3D NumPy数组

时间:2020-06-17 14:19:56

标签: python arrays pandas numpy

我有一个熊猫数据帧的numpy数组,我需要将其转换为形式(样本,行,列)的3D numpy数组,以便输入Keras模型进行训练。我的数据集中有46个样本,每个样本都是1101行x 64列。

这是我的数据帧的一维numpy数组的代码:

static_dfs = []
#read in static csvs as pandas df
#static files is my np array of csv files
for x in range(0, static_files.size):
  df = pd.read_csv(static_files[x], sep='\t', skiprows=skip_rows, header=(0))
  #append df to list  
  static_dfs.append(df)

#convert list to np array
static_dfs = np.asarray(static_dfs)

实际上,数组的形状为(46,)[样本数]。 如果我看一下数组中的一个数据框(例如,static_dfs [0]),则形状为(1101、64)。

然后我尝试将其转换为3D numpy数组:

static_nps = []

for x in range(0, static_dfs.size):
  static_nps.append(static_dfs[x].to_numpy())

#convert to numpy array
static_nps = np.asarray(static_nps)

但是它给了我这个错误:

could not broadcast input array from shape (1101,64) into shape (1101)

代码行:

    #convert to numpy array
    static_nps = np.asarray(static_nps)

最糟糕的是,我以前曾经工作过,但是我的一个协作者在我的一个数据文件中发现了一个错误之后,对我的代码进行了检查并对其进行了编辑。现在,我似乎无法像以前一样恢复工作,并陷入:(

我的3D阵列所需的形状看起来像(46,1101,64)。如果有人可以解决这个问题,那将是巨大的帮助!谢谢

0 个答案:

没有答案