python中如何dataframe转换为ndarray?
python中如何dataframe转换为ndarray?
小编介绍过python中ndarray与series如何相互转换的方法,其实Series转换为ndarray是一个一维数组,作为pandas另一个数据结构,DataFrame转换为ndarray是一个多维数组,那是怎么转换的呢?本文介绍python中dataframe转换为ndarray的两种方法。
1、利用dataframevalues属性将使用所有的列转换为ndarray对象。
frompandasimportSeries,DataFrame
fromnumpyimportarray
if__name__=='__main__':
data=[
[11.53,11.69,11.70,11.51,871365.0,000001],
[11.64,11.63,11.72,11.57,722764.0,000001],
[11.59,11.48,11.59,11.41,461808.0,000001],
[11.39,11.19,11.40,11.15,1074465.0,000001]]
df=DataFrame(data,index=["2017-10-18","2017-10-19","2017-10-20","2017-10-23"],
columns=["open","close","high","low","volume","code"])
printdf
printdf.as_matrix(['open','close'])
printdf.values
printarray(df)
2、通过values方法,实现dataframe转换为ndarray。
importpandasaspd
data=[['2019/08/01',10],
['2019/08/01',11]]
result=pd.DataFrame(data,columns=['ds','val'])
result.values
以上就是python中dataframe转换为ndarray的方法,希望能对你有所帮助哟~更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python包的导入方式有几种
python包的导入方式有几种本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、from...import导入frompackageimportmodule1,mo详情>>
2023-11-08 23:58:44
python参数是什么
python参数是什么1、概念Python中函数的参数是不变的对象。Python函数具有灵活的参数形式,但默认参数必须指向不变的对象。如果默认参数是可变...详情>>
2023-11-08 19:48:30
python使用items()遍历键值对
python使用items()遍历键值对字典可以用来存储各种方式的信息,所以有很多方式可以通过字典的所有键值对、键或值。说明1、即使通过字典,键值对...详情>>
2023-11-08 19:23:48
python按行读取文件的方法比较
python按行读取文件的方法比较1、read方法默认会把文件的所有内容一次性读取到内存。如果文件太大,对内存的占用会非常严重2、readline方法,re...详情>>
2023-11-08 18:22:27