26

总结 | DataFrame、Series、array、tensor的创建及相互转化

 5 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzA5OTIzNjE1NA%3D%3D&%3Bmid=2247488232&%3Bidx=1&%3Bsn=55f9e6e37c52a10d6a99b552f7649f6e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

↑ " 奶糖猫 "一个值得星标的公众号

本文约18张图,阅读大概需要5分钟

最近在入门图像识别,自然也会用到深度学习框架,也接触到了一个新的数据结构——tensor(张量)。除此之外,也有一些很常用的数据结构,比如DataFrame、Series、array等,这篇文章主要对这几种数据结构的创建及相互转换做一个小总结。

创建方法

DataFrame

这里就不在单独贴出每种数据结构的示例图,只是简单描述一下各个数据结构的特点。DataFrame类似于一个二维矩阵,但它的行列都有对应的索引。

DataFrame创建方法很多,这里给出比较常用的三种方法:

1、通过字典创建

3QBfMrn.png!mobile

2、通过元组创建

ry6juq.png!mobile原理与通过字典创建一致,但需要注意行、列索引需要自己指定。

3、randn随机生成

jQB3Iv.png!mobilenp.random.randn(m,n)是生成一个规格的矩阵,行列索引需要自己指定。

Series

Series 可以当成 DataFrame 中一个元素,一列索引对应一列值。

1、通过字典创建

eaAFRju.png!mobile

2、通过列表创建

qUB3I3n.png!mobile

3、通过arange创建

zq2auy2.png!mobile

array

JRniEfm.png!mobile

tensor

nI3EniE.png!mobile

这里 Tensor 是类,tensor 和 as_tensor则是方法,第一种生成的是浮点型,后两种生成数据的类型和传入数据类型一致,也就是说传入整型生成整型,传入浮点型生成浮点型。

转化

DataFrame 拆解 Series

bIfuiiB.png!mobile

索引出的单行或者单列的数据类型为Series。

DataFrame 转 array

1、直接获取values

qmm2Ujq.png!mobile

2、通过numpy转换

AVRRziI.png!mobile

Series 转 DataFrame

1、合成

IRBbme7.png!mobile

2、to_frame()方法

uQ3yeeY.png!mobile

Series 转 array

方法同DataFrame 转 array。

QZNJ73R.png!mobile

array 转 DataFrame

Rb2IBv7.png!mobile

array 转 Series

ZF7bEz2.png!mobile

array 转 tensor

mINZfiv.png!mobile

tensor 转 array

QZryuiN.png!mobile

上面这些创建及转化的方法只是一部分,也算是比较常用的一些,除此之外比如还可以通过列表作为中间介质进行转换等等,这里就不在过多介绍啦。

Read More

Flask从零到一 1 | 虚拟环境和第一个flask程序   

干!一张图整理了 Python 所有内置异常  

End

奶糖猫   

优秀的人都在看   

zm6RNj.png!mobile

在看点一下


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK