您现在的位置是:网站首页> 编程资料编程资料
pandas.DataFrame.from_dict直接从字典构建DataFrame的方法_python_
2023-05-26
282人已围观
简介 pandas.DataFrame.from_dict直接从字典构建DataFrame的方法_python_
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
到此这篇关于pandas.DataFrame.from_dict直接从字典构建DataFrame的方法的文章就介绍到这了,更多相关pandas字典构建DataFrame内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:
相关内容
- pandas函数isnull的具体使用_python_
- 浅谈pandas关于查看库或依赖库版本的API原理_python_
- Python实现校园网自动登录的脚本分享_python_
- 浅析python中的set类型_python_
- pandas温差查询案例的实现_python_
- 基于Python实现本地音乐播放器的制作_python_
- Python读取CSV文件并进行数据可视化绘图_python_
- Python通过psd-tools解析PSD文件_python_
- 一文搞懂Pandas数据透视的4个函数的使用_python_
- python中ThreadPoolExecutor线程池和ProcessPoolExecutor进程池_python_
