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函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






