ministep / SQL_DataAnalysis

SQL数据分析
9 stars 0 forks source link

使用示例— pantab 1.1.0文档 #58

Open kemistep opened 4 years ago

kemistep commented 4 years ago

潘塔布

使用模式

默认情况下,表将被写入 “公共” 模式。您可以通过指定tableauhyperapi.TableName读取 / 写入摘录时的方式来控制此行为。

import pandas as pd import pantab from tableauhyperapi import TableName

# Let's write somewhere besides the default public schema table \= TableName("not_the_public_schema","a_table")

df \= pd.DataFrame([ ["dog", 4], ["cat", 4], ], columns\=["animal", "num_of_legs"])

pantab.frame_to_hyper(df, "example.hyper", table\=table)

# Can also be round-tripped df2 \= pantab.frame_from_hyper("example.hyper", table\=table)

读写多个表

frames_to_hyper并且frames_from_hyper可以分别为 Hyper 提取编写和返回 DataFrames 字典。

import pandas as pd import pantab from tableauhyperapi import TableName

dict_of_frames \= { "table1": pd.DataFrame([[1, 2]], columns\=list("ab")), TableName("non_public_schema","table2"): pd.DataFrame([[3, 4]], columns\=list("cd")), }

pantab.frames_to_hyper(dict_of_frames, "example.hyper")

# Can also be round-tripped result \= pantab.frames_from_hyper("example.hyper")

注意

虽然您可以编写使用strtableauhyperapi.Nametableauhyperapi.TableName情况下,返回的字典的键frames_from_hyper将永远是tableauhyperapi.TableName实例

将数据追加到现有表

默认情况下,frame_to_hyper并且frames_to_hyper将全面下降并重新加载有针对性的表。但是,您还可以通过提供table_mode="a"作为关键字参数的方式将记录追加到现有表中。

import pandas as pd import pantab

df \= pd.DataFrame([ ["dog", 4], ["cat", 4], ], columns\=["animal", "num_of_legs"])

pantab.frame_to_hyper(df, "example.hyper", table\="animals")

new_data \= pd.DataFrame([["moose", 4]], columns\=["animal", "num_of_legs"])

# Instead of overwriting the animals table, we can append via table_mode pantab.frame_to_hyper(df, "example.hyper", table\="animals", table_mode\="a")

请注意,table_mode="a"如果表不存在,将创建该表。 https://pantab.readthedocs.io/en/latest/examples.html