如何将 pandas 数据帧列表转换为3d numpy数组? [英] How to convert a list of pandas dataframes into a 3d numpy array?

查看:61
本文介绍了如何将 pandas 数据帧列表转换为3d numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有pd.DataFrame的列表,例如:

If I have a list of pd.DataFrame like so:

df = pd.DataFrame(np.random.rand(4,5), columns = list('abcde'))
df_list = [df, df]

问题: 如何使用shape(2、3、5)将其转换为3D np.array?

Question: How can I convert it to a 3D np.array with shape (2, 3, 5)?

我尝试执行np.array(df_lsit),但是出现以下错误:

I tried to do np.array(df_lsit), but I get the following error:

ValueError: cannot copy sequence with size 4 to array axis with dimension 5

推荐答案

您应该将DataFrame转换为numpy数组,然后再将其转换为3D数组. 像这样:

You should convert your DataFrame to numpy array then convert it to 3D array. Like this:

np.array([np.array(df), np.array(df)])

这篇关于如何将 pandas 数据帧列表转换为3d numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆