熊猫DataFrame from_dict()–字典到DataFrame
Pandas DataFrame from_dict() method is used to convert Dict to DataFrame object. This method accepts the following parameters.
熊猫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 :数据的方向。 允许值为(“列”,“索引”),默认值为“列”。
- 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
。
Pandas DataFrame from_dict()示例 (Pandas DataFrame from_dict() Examples)
Let’s look at some examples to convert dict to DataFrame object.
让我们看一些将dict转换为DataFrame对象的示例。
1.从Dict创建DataFrame的简单示例 (1. Simple Example to create DataFrame from Dict)
import pandas as pdd1 = {'Name': ['Pankaj', 'Lisa'], 'ID': [1, 2]}df = pd.DataFrame.from_dict(d1)print(df)
Output:
输出:
Name ID
0 Pankaj 1
1 Lisa 2
2.从具有索引方向的Dict创建DataFrame (2. Creating DataFrame from Dict with index orientation)
import pandas as pdd1 = {'Name': ['Pankaj', 'Lisa'], 'ID': [1, 2]}df = pd.DataFrame.from_dict(d1, orient='index')print(df)
Output:
输出:
0 1
Name Pankaj Lisa
ID 1 2
3.将具有索引方向的Dict转换为Dict时,将标签分配给DataFrame列 (3. Assigning Labels to DataFrame Columns when converted Dict with index orientation)
import pandas as pdd1 = {'Name': ['Pankaj', 'Meghna'], 'ID': [1, 2], 'Role': ['CEO', 'CTO']}df = pd.DataFrame.from_dict(d1, columns=['A', 'B'], orient='index')print(df)
Output:
输出:
A B
Name Pankaj Meghna
ID 1 2
Role CEO CTO
什么时候使用DataFrame from_dict()方法? (When to use DataFrame from_dict() method?)
We can convert dictionary to DataFrame using its constructor too.
我们也可以使用其构造函数将字典转换为DataFrame。
import pandas as pdd1 = {'Name': ['Pankaj', 'Lisa'], 'ID': [1, 2]}df = pd.DataFrame(d1)
print(df)
Output:
输出:
Name ID
0 Pankaj 1
1 Lisa 2
But, there is no option to use index-based orientation.
但是,没有选择使用基于索引的方向。
So, when you want index orientation, use from_dict() method. For default scenarios, you are better off with the DataFrame constructor.
因此,当您想要索引方向时,请使用from_dict()方法。 对于默认方案,最好使用DataFrame构造函数。
参考资料 (References)
- Python Pandas Module TutorialPython Pandas模块教程
- pandas from_dict() API Docpandas from_dict()API文件
翻译自: https://www.journaldev.com/33472/pandas-dataframe-from_dict-convert-dictionary-to-dataframe
熊猫DataFrame from_dict()–字典到DataFrame相关推荐
- 熊猫to_csv()–将DataFrame转换为CSV
Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to wr ...
- [转载] python 把几个DataFrame合并成一个DataFrame——merge,append,join,conca
参考链接: Python | pandas 合并merge,联接join和级联concat python 把几个DataFrame合并成一个DataFrame--merge,append,join, ...
- python:DataFrame的创建以及DataFrame的属性
一.DataFrame的创建 Pandas 的数据结构主要是:Series(一维数组),DataFrame(二维数组).DataFrame是由索引和内容组成,索引既有行索引index又有列索引colu ...
- pandas索引复合索引dataframe数据、索引dataframe中指定行和指定列交叉格子的数据内容(getting a specific value)、使用元组tuple表达复合索引的指定行
pandas索引复合索引dataframe数据.索引dataframe中指定行和指定列交叉格子的数据内容(getting a specific value).使用元组tuple表达复合索引的指定行 目 ...
- Python中将dataframe转换为字典
有时候,在Python中需要将dataframe类型转换为字典类型,下面的方法帮助我们解决这一问题. 任务代码 # encoding: utf-8import pandas as pd a = ['N ...
- dataframe转字典dict格式
用到的方法主要有: my_dict = dict(zip(dataframe['列1'], dataframe['列2'])) 案例 import pandas as pd import numpy ...
- dataframe修改列名_python dataframe操作大全数据预处理过程(dataframe、md5)
hive表的特征选择,不同表之间的join 训练数据.测试数据的分开保存 使用pandas进行数据处理 显示所有列:pd.set_option('display.max_columns', None) ...
- dataframe 上下拼接_pandas DataFrame 的横向纵向拼接组合
concat 与其说是连接,更准确的说是拼接.就是把两个表直接合在一起.于是有一个突出的问题,是横向拼接还是纵向拼接,所以concat 函数的关键参数是axis . 函数的具体参数是: concat( ...
- 【Python数据处理篇——DataFrame数据准备】DataFrame的创建、增删改查、数据导入等
欢迎访问我搞事情的[知乎账号]:Coffee 以及我的[B站漫威剪辑账号]:VideosMan 若我的笔记对你有帮助,请用小小的手指,点一个大大的赞哦. 关于DataFrame的相关知识,我还进行了汇 ...
最新文章
- linux smart服务,RAKsmart Linux美国服务器常用信息命令
- Xamarin Essentials教程陀螺仪Gyroscope
- 一、activiti工作流(workflow)入门介绍
- JQuery选择器——基本筛选选择器和内容筛选选择器
- 修改服务器mysql密码,修改Linux服务器中的MySql密码
- 在LINUX 下安装 Realtek 8110SC 千兆网卡驱动
- ZOJ 3720 Magnet Darts (计算几何,概率,判点是否在多边形内)
- priority_queue实现大顶堆和小顶堆
- filter2D函数的.depth()变量的设定
- Apache站点下载大文件不完整原因及解决办法
- 查看硬盘分区表信息工具:fdisk0 for linux-0.11 附下载地址
- Mysql三种常见备份表方式
- 全球及中国IT运维管理产业十四五格局动向及项目价值收益预测报告2021-2027年
- ERROR2002(HY000):CantconnecttolocalMySQLserverthroughsocket/tmp/mys
- 使用table自带的deleteRow,insertRow方法实现表格内容滚动
- 根据交谈和3D面部表情判断抑郁症
- 步进电机主要技术参数
- PC端本地存储方案,Windows和Mac双端通用方案
- 19. OP-TEE中TA与CA执行流程详解-------软件架构篇
- flash 怎么擦掉fpga_基于FPGA的flash板卡程序擦除与固化
热门文章
- AJax错误WebForm1没有定义的javascript错误的解决方法
- [转载] 使用Python在Pandas Dataframe中建立索引
- Java BigDecimal 数据类型的运算
- 从后台获取的数据渲染到页面中的dom操作
- 8、第六 -面向对象高级语法-异常处理
- Ubuntu 16.04安装Caffe的记录及FCN官方代码的配置
- 定制属于自己的自动化安装的linux系统镜像
- CSS从零开始(1)--CSS基础语法
- (转)C++ 库汇总(网址+简介)
- 位运算和sizeof运算符