2012-11-29 38 views

回答

0

我不知道有任何直接的方式投了大熊貓據幀到gviz數據表。

這裏是我如何做到這一點: -

from gviz_data_table import Table # using https://bitbucket.org/charlie_x/gviz-data-table though you should probably use the official google one. 
import numpy as np 
import pandas as pd 

df = pd.DataFrame({ 
    "time" : [1,2,3,4,5], 
    "temp" : np.random.rand(5) 
}) 

table = Table() 
table.add_column('temp', float, 'Temp') 
table.add_column('time', float, 'Time') 

for row in df.iterrows(): 
    table.append(row[1].tolist()) 

table.rows 

[OrderedDict([('temp', <gviz_data_table.cell.Cell object at 0x104a64aa0>), ('time', <gviz_data_table.cell.Cell object at 0x104a64a50>)]), 
OrderedDict([('temp', <gviz_data_table.cell.Cell object at 0x104a64af0>), ('time', <gviz_data_table.cell.Cell object at 0x104a64b40>)]), 
OrderedDict([('temp', <gviz_data_table.cell.Cell object at 0x104a64b90>), ('time', <gviz_data_table.cell.Cell object at 0x104a64be0>)]), 
OrderedDict([('temp', <gviz_data_table.cell.Cell object at 0x104a64c30>), ('time', <gviz_data_table.cell.Cell object at 0x104a64c80>)]), 
OrderedDict([('temp', <gviz_data_table.cell.Cell object at 0x104a64cd0>), ('time', <gviz_data_table.cell.Cell object at 0x104a64d20>)])] 
0

作爲替代使用iterrows和列表,您可以使用.values獲得數據框作爲numpy的對象(數組的數組 - 每行一個)

要完成該過程,並把它變成一個GViz表

  • 成立了描述
  • 使用gviz_api的LoadData()我請加載這些值。

代碼示例

#set up Google Table w/ the description 
gpa_desc=[("grade_date", "date", "Date"), 
      ("gpa", "number", "GPA")] 
gpa_data_table=gviz_api.DataTable(gpa_desc) 

#your df as a numpy object 
gpa_values=df_gpa.values 

#populate the table with your data 
gpa_data_table.LoadData(gpa_values) 

#convert to json to pass to the view 
gpa_json=gpa_data_table.ToJSon()