pandas.DataFrame.from_dict的使用介绍

Pandas几个星期不用,差不多又快全部忘记了。便于记忆,还是写一些痕迹,加深记忆。

参考链接:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html

这是一个类方法,创建一个df对象。

Parameters

datadict

Of the form {field : array-like} or {field : dict}.

orient{‘columns’, ‘index’}, default ‘columns’

The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’.

dtypedtype, default None

Data type to force, otherwise infer.

columnslist, default None

Column labels to use when orient='index'. Raises a ValueError if used with orient='columns'.

对于参数的设置,datadict在默认下,传入参数为{key: {key: value}}或者{key:[...]}

这样的情况下,使用默认的orient属性,key将当做columns使用。

In [22]: mydict = {'name':{1:'sidian'},'age':{2:'18'}}                                    

In [23]: pd.DataFrame.from_dict(mydict)                                                   
Out[23]: 
     name  age
1  sidian  NaN
2     NaN   18

  这是使用了字典嵌套字典的写法,外层字典的key为columns,values内的dict的keys为rows的名称,缺省的值用了NAN

当修改orient的默认值'columns'为'index',内部的key为columns,外部的key为rows

In [25]: pd.DataFrame.from_dict(mydict,orient='index')                                                          
Out[25]: 
           1    2
name  sidian  NaN
age      NaN   18

  当时使用字典嵌套字典的时候,设置了orient='index'后,通过设置columns只会显式,在原df中已经存在的columns

In [29]: pd.DataFrame.from_dict(mydict,orient='index', columns=[2,3])                                           
Out[29]: 
      2    3
age  18  NaN

In [30]: pd.DataFrame.from_dict(mydict,orient='index', columns=[3,4])                                           
Out[30]: 
Empty DataFrame
Columns: [3, 4]
Index: []

  pd.DataFrame.from_dict的类方法在使用字典套字典的形式还是比较特别的,下面使用链接中的案例key与list的形式。

In [32]: data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}                                          

In [33]: pd.DataFrame.from_dict(data)                                                                           
Out[33]: 
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

  这个rows的编号,自动生成,keys为columns的名称。

设置orient='index',key将变成rows的index,rows会变成默认的0,1,2,3...

In [34]: pd.DataFrame.from_dict(data,orient='index')                                                            
Out[34]: 
       0  1  2  3
col_1  3  2  1  0
col_2  a  b  c  d

  通过设置columns,来替换显式的默认columns的名称.

In [35]: pd.DataFrame.from_dict(data,orient='index',columns=list('abcd'))                                       
Out[35]: 
       a  b  c  d
col_1  3  2  1  0
col_2  a  b  c  d

  

工作中碰到,就是一个普通的dict,也就是说value是不可迭代的对象,必须设置orient='index',要不然会报错,也就是dict的key不能用于columns。

In [41]: new_dict                                                                                               
Out[41]: {'name': 'sidian', 'age': 18}

In [42]: pd.DataFrame.from_dict(new_dict, orient='index')                                                       
Out[42]: 
           0
name  sidian
age       18

In [43]: pd.DataFrame.from_dict(new_dict, orient='index', columns=['info'])                                     
Out[43]: 
        info
name  sidian
age       18

In [44]:         

  

 

posted @ 2021-01-11 11:22  就是想学习  阅读(4986)  评论(0编辑  收藏  举报