pandas.DataFrame.from_dict怎么直接从字典构建DataFrame

发布时间:2022-06-17 14:11:53 作者:iii
来源:亿速云 阅读:240

这篇文章主要介绍“pandas.DataFrame.from_dict怎么直接从字典构建DataFrame”,在日常操作中,相信很多人在pandas.DataFrame.from_dict怎么直接从字典构建DataFrame问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”pandas.DataFrame.from_dict怎么直接从字典构建DataFrame”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

pandas函数中pandas.DataFrame.from_dict 直接从字典构建DataFrame 。

参数解析

DataFrame from_dict()方法用于将Dict转换为DataFrame对象。 此方法接受以下参数。

实例  

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怎么直接从字典构建DataFrame”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. python中如何实现DataFrame转dict字典
  2. pandas中怎么通过字典生成dataframe

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

dataframe

上一篇:pandas函数isnull怎么使用

下一篇:.Net如何通过TaskFactory.FromAsync简化APM

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》