verticapy.vDataFrame.to_db¶
- vDataFrame.to_db(name: str, usecols: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, relation_type: Literal['view', 'temporary', 'table', 'local', 'insert'] = 'view', inplace: bool = False, db_filter: Annotated[str | list[str] | StringSQL | list[StringSQL], ''] = '', nb_split: int = 0, order_by: None | Annotated[str | list[str], 'STRING representing one column or a list of columns'] | dict = None, segmented_by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None) vDataFrame¶
Saves the
vDataFramecurrent relation to the Vertica database.Parameters¶
- name: str
Name of the relation. To save the relation in a specific schema, you can write
'"my_schema"."my_relation"'. Use double quotes ‘”’ to avoid errors due to special characters.- usecols: SQLColumns, optional
vDataColumnto select from the finalvDataFramerelation. If empty, allvDataColumnare selected.- relation_type: str, optional
Type of the relation.
- view:
View.
- table:
Table.
- temporary:
Temporary Table.
- local:
Local Temporary Table.
- insert:
Inserts into an existing table.
- inplace: bool, optional
If set to
True, thevDataFrameis replaced with the new relation.- db_filter: SQLExpression, optional
Filter used before creating the relation in the DB. It can be a
listof conditions or an expression. This parameter is useful for creating train and test sets on TS.- nb_split: int, optional
If this parameter is greater than 0, it adds a new column
'_verticapy_split_'to the final relation. This column contains values in[0;nb_split - 1]where each category represents1 / nb_splitof the entire distribution.- order_by: SQLColumns | dict, optional
List of the
vDataColumnused to sort the data, using asc order or adictionaryof all sorting methods. For example, to sort by “column1” ASC and “column2” DESC, write:{"column1": "asc", "column2": "desc"}- segmented_by: SQLColumns, optional
This parameter is only used when
relation_typeis ‘table’ or ‘temporary’. Otherwise, it is ignored. List of the vDataColumns used to segment the data; All the columns used will be passed to the HASH function.
Returns¶
- vDataFrame
self
Examples¶
We import
verticapy:import verticapy as vp
Hint
By assigning an alias to
verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromverticapyare used as intended without interfering with functions from other libraries.For this example, we will use the Titanic dataset.
import verticapy.datasets as vpd data = vpd.load_titanic()
123pclass123survivedAbcAbcsex123age123sibsp123parchAbcticket123fareAbccabinAbcembarkedAbcboat123bodyAbc1 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 2 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] 3 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] 4 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] 5 1 0 male 27.0 1 0 13508 136.7792 C89 C [null] [null] 6 1 0 male 37.0 1 1 PC 17756 83.1583 E52 C [null] [null] 7 1 0 male 31.0 1 0 F.C. 12750 52.0 B71 S [null] [null] 8 1 0 male 50.0 1 0 PC 17761 106.425 C86 C [null] 62 9 1 0 female 36.0 0 0 PC 17531 31.6792 A29 C [null] [null] 10 1 0 male 37.0 1 0 113803 53.1 C123 S [null] [null] 11 1 0 male 24.0 0 0 PC 17593 79.2 B86 C [null] [null] 12 1 0 male 45.0 1 0 36973 83.475 C83 S [null] [null] 13 1 0 male 40.0 0 0 112059 0.0 B94 S [null] 110 14 1 0 male 42.0 0 0 113038 42.5 B11 S [null] [null] 15 1 0 male [null] 0 0 17463 51.8625 E46 S [null] [null] 16 1 0 male 42.0 1 0 113789 52.0 [null] S [null] 38 17 1 0 male [null] 0 0 PC 17600 30.6958 [null] C 14 [null] 18 1 0 male 29.0 0 0 113501 30.0 D6 S [null] 126 19 1 0 male 46.0 0 0 13050 75.2417 C6 C [null] 292 20 1 0 male 54.0 0 0 17463 51.8625 E46 S [null] 175 21 1 0 male 47.0 0 0 113796 42.4 [null] S [null] [null] 22 1 0 male 58.0 0 2 35273 113.275 D48 C [null] 122 23 1 0 male 45.5 0 0 113043 28.5 C124 S [null] 166 24 1 0 male 29.0 1 0 113776 66.6 C2 S [null] [null] 25 1 0 male 47.0 0 0 110465 52.0 C110 S [null] 207 26 1 0 male 38.0 0 0 19972 0.0 [null] S [null] [null] 27 1 0 male 22.0 0 0 PC 17760 135.6333 [null] C [null] 232 28 1 0 male 31.0 0 0 PC 17590 50.4958 A24 S [null] [null] 29 1 0 male 50.0 1 0 13507 55.9 E44 S [null] [null] 30 1 0 male 56.0 0 0 17764 30.6958 A7 C [null] [null] 31 1 0 male 57.0 1 0 PC 17569 146.5208 B78 C [null] [null] 32 1 0 female 63.0 1 0 PC 17483 221.7792 C55 C57 S [null] [null] 33 1 0 male 61.0 0 0 36963 32.3208 D50 S [null] 46 34 1 0 male 21.0 0 1 35281 77.2875 D26 S [null] 169 35 1 0 male 51.0 0 1 PC 17597 61.3792 [null] C [null] [null] 36 1 1 female 63.0 1 0 13502 77.9583 D7 S 10 [null] 37 1 1 female 32.0 0 0 11813 76.2917 D15 C 8 [null] 38 1 1 female 58.0 0 0 113783 26.55 C103 S 8 [null] 39 1 1 female 44.0 0 0 PC 17610 27.7208 B4 C 6 [null] 40 1 1 female 41.0 0 0 16966 134.5 E40 C 3 [null] 41 1 1 female 53.0 0 0 PC 17606 27.4458 [null] C 6 [null] 42 1 1 male 36.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 43 1 1 female 58.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 44 1 1 male 11.0 1 2 113760 120.0 B96 B98 S 4 [null] 45 1 1 female 76.0 1 0 19877 78.85 C46 S 6 [null] 46 1 1 female [null] 0 1 113505 55.0 E33 S 6 [null] 47 1 1 female 39.0 1 1 PC 17756 83.1583 E49 C 14 [null] 48 1 1 female 27.0 1 2 F.C. 12750 52.0 B71 S 3 [null] 49 1 1 female [null] 0 0 17421 110.8833 [null] C 4 [null] 50 1 1 female 35.0 0 0 113503 211.5 C130 C 4 [null] 51 1 1 female 22.0 0 1 112378 59.4 [null] C 7 [null] 52 1 1 female 25.0 1 0 11765 55.4417 E50 C 5 [null] 53 1 1 male 48.0 1 0 PC 17572 76.7292 D33 C 3 [null] 54 1 1 female 35.0 1 0 36973 83.475 C83 S D [null] 55 1 1 male 27.0 0 0 PC 17572 76.7292 D49 C 3 [null] 56 1 1 female 24.0 0 0 11767 83.1583 C54 C 7 [null] 57 1 1 female 52.0 1 1 12749 93.5 B69 S 3 [null] 58 1 1 female 44.0 0 1 111361 57.9792 B18 C 4 [null] 59 1 1 female 15.0 0 1 24160 211.3375 B5 S 2 [null] 60 1 1 male 30.0 1 0 13236 57.75 C78 C 11 [null] 61 1 1 female 31.0 1 0 35273 113.275 D36 C 6 [null] 62 1 1 female 39.0 0 0 PC 17758 108.9 C105 C 8 [null] 63 1 1 female 22.0 0 1 113509 61.9792 B36 C 5 [null] 64 1 1 male 52.0 0 0 113786 30.5 C104 S 6 [null] 65 1 1 female 43.0 0 1 24160 211.3375 B3 S 2 [null] 66 1 1 female 33.0 0 0 110152 86.5 B77 S 8 [null] 67 1 1 male 45.0 1 1 16966 134.5 E34 C 3 [null] 68 1 1 female 40.0 1 1 16966 134.5 E34 C 3 [null] 69 1 1 male 48.0 1 0 19996 52.0 C126 S 5 7 [null] 70 1 1 female [null] 0 0 PC 17585 79.2 [null] C D [null] 71 1 1 female 35.0 0 0 PC 17755 512.3292 [null] C 3 [null] 72 1 1 female 60.0 1 0 110813 75.25 D37 C 5 [null] 73 1 1 male 21.0 0 1 PC 17597 61.3792 [null] C A [null] 74 2 0 male 23.0 0 0 C.A. 31030 10.5 [null] S [null] [null] 75 2 0 male 28.0 0 0 244358 26.0 [null] S [null] [null] 76 2 0 male 60.0 1 1 29750 39.0 [null] S [null] [null] 77 2 0 female 44.0 1 0 244252 26.0 [null] S [null] [null] 78 2 0 male 29.0 1 0 2003 26.0 [null] S [null] [null] 79 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 80 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 81 2 0 male 54.0 0 0 28403 26.0 [null] S [null] [null] 82 2 0 male 18.0 0 0 236171 13.0 [null] S [null] [null] 83 2 0 male 36.0 0 0 229236 13.0 [null] S [null] 236 84 2 0 male 34.0 1 0 28664 21.0 [null] S [null] [null] 85 2 0 male 21.0 1 0 28133 11.5 [null] S [null] [null] 86 2 0 male 21.0 1 0 28134 11.5 [null] S [null] [null] 87 2 0 male 24.0 0 0 233866 13.0 [null] S [null] 155 88 2 0 male 34.0 0 0 12233 13.0 [null] S [null] [null] 89 2 0 male 30.0 0 0 250653 13.0 [null] S [null] 75 90 2 0 male 44.0 0 0 248746 13.0 [null] S [null] 35 91 2 0 male 49.0 1 2 220845 65.0 [null] S [null] [null] 92 2 0 male 21.0 2 0 S.O.C. 14879 73.5 [null] S [null] [null] 93 2 0 male 21.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 94 2 0 female 60.0 1 0 24065 26.0 [null] S [null] [null] 95 2 0 male 24.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 96 2 0 male 22.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 97 2 0 male 35.0 0 0 233734 12.35 [null] Q [null] [null] 98 2 0 male 31.0 0 0 C.A. 18723 10.5 [null] S [null] 165 99 2 0 male 36.0 0 0 SC/Paris 2163 12.875 D C [null] [null] 100 2 0 male [null] 0 0 SC/A.3 2861 15.5792 [null] C [null] [null] Rows: 1-100 | Columns: 14Note
VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.
Let’s do some transformations.
data.get_dummies() data.normalize()
123pclass100%... 123survived100%123embarked_Q100%1 -1.5245848565393982 ... -0.7573073711539632 0 2 -1.5245848565393982 ... -0.7573073711539632 0 3 -1.5245848565393982 ... -0.7573073711539632 0 4 -1.5245848565393982 ... -0.7573073711539632 0 5 -1.5245848565393982 ... -0.7573073711539632 0 6 -1.5245848565393982 ... -0.7573073711539632 0 7 -1.5245848565393982 ... -0.7573073711539632 0 8 -1.5245848565393982 ... -0.7573073711539632 0 9 -1.5245848565393982 ... -0.7573073711539632 0 10 -1.5245848565393982 ... -0.7573073711539632 0 11 -1.5245848565393982 ... -0.7573073711539632 0 12 -1.5245848565393982 ... -0.7573073711539632 0 13 -1.5245848565393982 ... -0.7573073711539632 0 14 -1.5245848565393982 ... -0.7573073711539632 0 15 -1.5245848565393982 ... -0.7573073711539632 0 16 -1.5245848565393982 ... -0.7573073711539632 0 17 -1.5245848565393982 ... -0.7573073711539632 0 18 -1.5245848565393982 ... -0.7573073711539632 0 19 -1.5245848565393982 ... -0.7573073711539632 0 20 -1.5245848565393982 ... -0.7573073711539632 0 Let’s save the result in the Database.
data.to_db( name = '"public"."data_normalized"', usecols = ["fare", "sex", "survived"], relation_type = "table", ) vp.vDataFrame('"public"."data_normalized"')
123fare99%... Abcsex100%123survived100%1 -0.5849058045380454 ... male 1.3193977310771285 2 -0.5226181577262721 ... male -0.7573073711539632 3 -0.5217481973017847 ... male -0.7573073711539632 4 -0.5217481973017847 ... male -0.7573073711539632 5 -0.5217481973017847 ... male -0.7573073711539632 6 -0.5131207731794659 ... male -0.7573073711539632 7 -0.5126459039521256 ... male -0.7573073711539632 8 -0.5097966885880838 ... male -0.7573073711539632 9 -0.5097966885880838 ... male -0.7573073711539632 10 -0.5097966885880838 ... male -0.7573073711539632 11 -0.5097966885880838 ... male -0.7573073711539632 12 -0.5078174336485294 ... male -0.7573073711539632 13 -0.5078174336485294 ... male -0.7573073711539632 14 -0.5078174336485294 ... male -0.7573073711539632 15 -0.5078174336485294 ... male -0.7573073711539632 16 -0.5078174336485294 ... male -0.7573073711539632 17 -0.5078174336485294 ... male -0.7573073711539632 18 -0.5078174336485294 ... male -0.7573073711539632 19 -0.5078174336485294 ... male -0.7573073711539632 20 -0.5078174336485294 ... male -0.7573073711539632 Let’s add a split column in the final relation.
data.to_db( name = '"public"."data_norm_split"', usecols = ["fare", "sex", "survived"], relation_type = "table", nb_split = 3, ) vp.vDataFrame('"public"."data_norm_split"')
123fare99%... Abcsex100%123_verticapy_split_100%1 -0.6451344183800711 ... male 1.0 2 -0.6451344183800711 ... male 1.0 3 -0.6451344183800711 ... male 1.0 4 -0.6451344183800711 ... male 1.0 5 -0.6451344183800711 ... male 2.0 6 -0.6451344183800711 ... male 2.0 7 -0.5849058045380454 ... male 1.0 8 -0.5148625935053502 ... male 1.0 9 -0.5121710347247853 ... male 2.0 10 -0.5112212962701047 ... male 1.0 11 -0.5112212962701047 ... male 1.0 12 -0.5112212962701047 ... male 1.0 13 -0.5112212962701047 ... male 1.0 14 -0.5111415182399116 ... male 0.0 15 -0.5097966885880838 ... male 1.0 16 -0.5078972116787226 ... male 1.0 17 -0.5078972116787226 ... male 1.0 18 -0.5078972116787226 ... male 1.0 19 -0.5078972116787226 ... male 1.0 20 -0.5078972116787226 ... male 1.0 Let’s use conditions to filter data.
data.to_db( name = '"public"."data_norm_filter"', usecols = ["fare", "sex", "survived"], relation_type = "table", db_filter = "sex = 'female'", ) vp.vDataFrame('"public"."data_norm_filter"')
123fare100%... Abcsex100%123survived100%1 -0.5017239117232986 ... female -0.7573073711539632 2 -0.49839982713191644 ... female 1.3193977310771285 3 -0.49824217054843944 ... female 1.3193977310771285 4 -0.49792495790457614 ... female -0.7573073711539632 5 -0.49792495790457614 ... female -0.7573073711539632 6 -0.49792495790457614 ... female -0.7573073711539632 7 -0.49792495790457614 ... female -0.7573073711539632 8 -0.49792495790457614 ... female -0.7573073711539632 9 -0.49792495790457614 ... female -0.7573073711539632 10 -0.49792495790457614 ... female -0.7573073711539632 11 -0.49792495790457614 ... female -0.7573073711539632 12 -0.49792495790457614 ... female -0.7573073711539632 13 -0.49792495790457614 ... female -0.7573073711539632 14 -0.49792495790457614 ... female -0.7573073711539632 15 -0.49792495790457614 ... female -0.7573073711539632 16 -0.4972126540635657 ... female 1.3193977310771285 17 -0.4951555205707275 ... female -0.7573073711539632 18 -0.49222652717649246 ... female 1.3193977310771285 19 -0.49222652717649246 ... female 1.3193977310771285 20 -0.49222652717649246 ... female 1.3193977310771285 Note
The
vDataFrame.to_db()method enables you to save thevDataFrameinto various types of relations, including views, temporary tables, temporary local tables, and regular tables. It also allows for inserting elements into an existing table, as well as ordering and segmenting the data using theorder_byandsegmented_byparameters.See also