pandas.DataFrame.from_dict直接从字典构建DataFrame的方法
作者:旺仔的算法coding笔记 发布时间:2022-02-22 03:13:28
pandas函数中pandas.DataFrame.from_dict 直接从字典构建DataFrame 。
参数解析
DataFrame from_dict()方法用于将Dict转换为DataFrame对象。 此方法接受以下参数。
data: dict or array like object to create DataFrame.data :字典或类似数组的对象来创建DataFrame。
orient: The orientation of the data. The allowed values are (‘columns’, ‘index’), default is the ‘columns’. orient :数据的方向。 允许值为(“列”,“索引”),默认值为“列”。 Specify orient='index' to create the DataFrame using dictionary keys as rows:。 当参数orient为index值时,会将字典的keys作为DataFrame的行。(默认是keys变为列)
columns: a list of values to use as labels for the DataFrame when orientation is ‘index’. If it’s used with columns orientation, ValueError is raised. columns :当方向为“索引”时,用作DataFrame标签的值的列表。 如果与列方向一起使用,则会引发ValueError 。
实例
1)By default the keys of the dict become the DataFrame columns:
默认是将字典的keys作为列
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data)
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
2) Specify orient='index' to create the DataFrame using dictionary keys as rows: 参数orient为index值时,会将字典的keys作为DataFrame的行
data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data, orient='index')
0 1 2 3
row_1 3 2 1 0
row_2 a b c d
3) orient为index值时, 可以手动命名列名
pd.DataFrame.from_dict(data, orient='index',
columns=['A', 'B', 'C', 'D'])
A B C D
row_1 3 2 1 0
row_2 a b c d
参考: pandas.DataFrame.from_dict — pandas 1.3.4 documentation
来源:https://blog.csdn.net/wangwangstone/article/details/121304120


猜你喜欢
- 在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict:d = dict(name='Bob', age=20,
- 目录一、🌕月亮二、🌕雪花月饼一、🌕月亮导入库matplotlib和numpy,作为工具直接用。from mpl_toolkits.mplot
- 本文实例讲述了Django框架反向解析操作。分享给大家供大家参考,具体如下:1. 定义:随着功能的增加会出现更多的视图,可能之前配置的正则表
- 推荐go学习书籍,点击链接跳转京东官方商城购买。服务端经常需要返回一个列表,里面包含很多用户数据,常规做法当然是遍历然后读缓存。使用Go语言
- 目录1. format格式化_填充符号使用1.1 format格式化1.2 format的填充符号的使用2. 字符串相关的方法3. 列表的相
- 今早开机发现,打开SQL Server 2008 的 SQL Server Management Studio,输入sa的密码发现,无法登陆
- 一、logging日志模块等级常见log级别从高到低:CRITICAL 》ERROR 》WARNING 》INFO 》DEBUG,默认等级为
- 在开发过程中,收到这样一个问题反馈,在网站上传 100 MB 以上的文件经常失败,重试也要等老半天,这就难为需要上传大规格文件的用户了。那么
- 实例如下:function getQueStr(url, ref) //取获参数值{ var str = url.substr(
- MySQL基本增删改查语句练习创建数据库:1、在cmd窗口中输入mysql -u root -p登录MySQL环境2、创建数据库为了便于在命
- 如下所示:#获取一个值在某个区间的指定倍数的值方法#1# print([i for i in range(1,101) if i%5==0]
- 一个拖动层和Onmouse自动下拉效果,IE支持,不支持ff。下面所示的效果四个小块可以拖动到页面任意点,大黑块可以连同四个小块随动。<
- 最近一句话后门不断升级大家注意防范,基本上多事字符替换过护卫神PHP一句话作者:小东 <?php $a = str_replace(x
- #创建触发器,当往order表中添加记录是,更新goods表 delimiter $ CREATE TRIGGER trigger1 AFT
- 一、定义新的自动求导函数在底层,每个原始的自动求导运算实际上是两个在Tensor上运行的函数。其中,forward函数计算从输入Tensor
- Go 编写定时器和定时任务在 项目开发当中,可能会遇到这样的场景:1 A任务需要在多久之后执行一次(定时器)2.B任务需要每隔多长时间执行一
- 本文实例讲述了python实现查找两个字符串中相同字符并输出的方法。分享给大家供大家参考。具体实现方法如下:seq1 = "spa
- 1、先看看什么是 iterable 对象以内置的max函数为例子,查看其doc:>>> print max.__doc__
- 正题: 1.1 javascript的灵活性 面向对象对象的Javascript编程模式:1、可以保存状态 2、具有对象内部才能调用的方法
- 目录一、介绍1.什么是索引?2.为什么要有索引呢?二、索引的原理一 索引原理二 磁盘IO与预读三、索引的数据结构四、Mysql索引管理一、功