Pandas dataframe:修订间差异

来自牛奶河Wiki
跳到导航 跳到搜索
(创建页面,内容为“== pandas dataframe conversion == ===dict=== ====dict -> dataframe==== d1 = {"columns":["Apple","Pear"],"data":[[12,0], [8,7], [1, 9]]} df2 = pd.DataFrame(d1['data']) Apple Pear 0 12 0 1 8 7 2 1 9 df2.columns=d1['columns'] Index(['Apple', 'Pear'], dtype='object') ====dataframe -> dict==== Syntax: DataFrame.to_dict(orient=’dict’, into=)\\ Parameters: * orient: String value, (‘dict’, ‘list’, ‘series’, ‘split’…”)
 
无编辑摘要
第1行: 第1行:
== pandas dataframe conversion ==
===pandas dataframe conversion===
 
===dict ===
===dict===
==== dict -> dataframe====
====dict -> dataframe====
d1 = {"columns":["Apple","Pear"],"data":[[12,0], [8,7], [1, 9]]}
d1 = {"columns":["Apple","Pear"],"data":[[12,0], [8,7], [1, 9]]} df2 = pd.DataFrame(d1['data'])
df2 = pd.DataFrame(d1['data'])
  Apple  Pear
    Apple  Pear
0    12    0 1      8    7 2      1    9
0    12    0
 
1      8    7
df2.columns=d1['columns'] Index(['Apple', 'Pear'], dtype='object')
2      1    9
df2.columns=d1['columns']
Index(['Apple', 'Pear'], dtype='object')
====dataframe -> dict====
====dataframe -> dict====
Syntax: DataFrame.to_dict(orient=’dict’, into=)\\
Syntax: DataFrame.to_dict(orient=’dict’, into=)


Parameters:
Parameters:
  * orient: String value, (‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’) Defines which dtype to convert Columns(series into). For example, ‘list’ would return a dictionary of lists with Key=Column name and Value=List (Converted series).
* orient: String value, (‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’) Defines which dtype to convert Columns(series into). For example, ‘list’ would return a dictionary of lists with Key=Column name and Value=List (Converted series).
  * into: class, can pass an actual class or instance. For example in case of defaultdict instance of class can be passed. Default value of this parameter is dict.
* into: class, can pass an actual class or instance. For example in case of defaultdict instance of class can be passed. Default value of this parameter is dict.
====Example====
==== Example====
... df2.to_dict('split') {'index': [0, 1, 2], 'columns': ['Apple', 'Pear'], 'data': [[12, 0], [8, 7], [1, 9]]}
...
 
df2.to_dict('split')
df2.to_dict('records') [{'Apple': 12, 'Pear': 0}, {'Apple': 8, 'Pear': 7}, {'Apple': 1, 'Pear': 9}]
{'index': [0, 1, 2], 'columns': ['Apple', 'Pear'], 'data': [[12, 0], [8, 7], [1, 9]]}
df2.to_dict('records')
[{'Apple': 12, 'Pear': 0}, {'Apple': 8, 'Pear': 7}, {'Apple': 1, 'Pear': 9}]
===list===
===list===
====list -> dataframe====
====list -> dataframe====
  See also: dict -> dataframe
  See also: dict -> dataframe
====dataframe -> list====
====dataframe -> list====
    a1 = df1.values    # values方法将dataframe转为numpy.ndarray
a1 = df1.values    # values方法将dataframe转为numpy.ndarray
    l1 = a1.tolist()
l1 = a1.tolist()
    l1[0]              # get frist value
l1[0]              # get frist value
    usys.utime(l1[0][7].value/10**9)  
usys.utime(l1[0][7].value/10**9) * P.S. 日期型的字段转换后格式:Timestamp('2017-04-13 13:48:32'),pandas._libs.tslibs.timestamps.Timestamp. 可以使用 usys.utime(l1[0][7].value/10**9) 转换。
  * P.S. 日期型的字段转换后格式:Timestamp('2017-04-13 13:48:32'),pandas._libs.tslibs.timestamps.Timestamp. 可以使用 usys.utime(l1[0][7].value/10**9) 转换。
[[分类:Develop]]
[[分类:Python]]
[[分类:Pandas]]

2022年12月28日 (三) 13:10的版本

pandas dataframe conversion

dict

dict -> dataframe

d1 = {"columns":["Apple","Pear"],"data":[[12,0], [8,7], [1, 9]]}
df2 = pd.DataFrame(d1['data'])
   Apple  Pear
0     12     0
1      8     7
2      1     9

df2.columns=d1['columns']
Index(['Apple', 'Pear'], dtype='object')

dataframe -> dict

Syntax: DataFrame.to_dict(orient=’dict’, into=)

Parameters:

  • orient: String value, (‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’) Defines which dtype to convert Columns(series into). For example, ‘list’ would return a dictionary of lists with Key=Column name and Value=List (Converted series).
  • into: class, can pass an actual class or instance. For example in case of defaultdict instance of class can be passed. Default value of this parameter is dict.

Example

...
df2.to_dict('split')
{'index': [0, 1, 2], 'columns': ['Apple', 'Pear'], 'data': [[12, 0], [8, 7], [1, 9]]}

df2.to_dict('records')
[{'Apple': 12, 'Pear': 0}, {'Apple': 8, 'Pear': 7}, {'Apple': 1, 'Pear': 9}]

list

list -> dataframe

  See also: dict -> dataframe

dataframe -> list

a1 = df1.values    # values方法将dataframe转为numpy.ndarray
l1 = a1.tolist()
l1[0]              # get frist value
usys.utime(l1[0][7].value/10**9) * P.S. 日期型的字段转换后格式:Timestamp('2017-04-13 13:48:32'),pandas._libs.tslibs.timestamps.Timestamp. 可以使用 usys.utime(l1[0][7].value/10**9) 转换。