Python 实现使用dict 创建二维数据、DataFrame

yipeiwu_com7年前Python基础

Python 实现使用 dict 创建二维数据

dict 的 keys、values 分别作为二维数据的两列

In [16]: d = {1:'aa', 2:'bb', 3:'cc'}
In [17]: arr = list(d.items()) # 关键的一步
In [18]: narr = np.array(arr)
In [19]: narr
Out[19]:
array([['1', 'aa'],
  ['2', 'bb'],
  ['3', 'cc']],
  dtype='<U11')

Python 实现使用 dict 创建DataFrame对象

dict 的 keys、values 分别作为 DataFrame 对象的两列

In [16]: d = {1:'aa', 2:'bb', 3:'cc'}
In [17]: arr = list(d.items()) # 关键的一步
In [22]: df = pd.DataFrame(list(d.items()), columns=['id', 'chars'])
In [23]: df
Out[23]:
 id chars
0 1 aa
1 2 bb
2 3 cc

以上这篇Python 实现使用dict 创建二维数据、DataFrame就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Windows系统下使用flup搭建Nginx和Python环境的方法

首先确保你的电脑里已经安装了Python和Django,接下来我们还需要两个组件,nginx服务器和flup(Python的FastCGI组件) nginx下载地址:http://ngi...

Python linecache.getline()读取文件中特定一行的脚本

Python linecache.getline()读取文件中特定一行的脚本

比如: ˂!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHig...

详解Python中的join()函数的用法

函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:     join(): &n...

Python的Django框架中使用SQLAlchemy操作数据库的教程

零、SQLAlchemy是什么? SQLAlchemy的官网上写着它的介绍文字: SQLAlchemy is the Python SQL toolkit and Object Rela...

Python使用Matplotlib实现雨点图动画效果的方法

Python使用Matplotlib实现雨点图动画效果的方法

本文实例讲述了Python使用Matplotlib实现雨点图动画效果的方法。分享给大家供大家参考,具体如下: 关键点 win10安装ffmpeg animation函数使用 update...