栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas.DataFrame.from

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas.DataFrame.from

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/499359.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号