verticapy.read_csv#
- verticapy.read_csv(path: str, schema: str | None = None, table_name: str | None = None, sep: str | None = None, header: bool = True, header_names: list | None = None, dtype: dict | None = None, na_rep: str | None = None, quotechar: str = '"', escape: str = '\x17', record_terminator: str = '\n', trim: bool = True, omit_empty_keys: bool = False, reject_on_duplicate: bool = False, reject_on_empty_key: bool = False, reject_on_materialized_type_error: bool = False, parse_nrows: int = -1, insert: bool = False, temporary_table: bool = False, temporary_local_table: bool = True, gen_tmp_table_name: bool = True, ingest_local: bool = True, genSQL: bool = False, materialize: bool = True) vDataFrame #
Ingests a CSV file using flex tables.
Parameters#
- path: str
Absolute path where the CSV file is located.
- schema: str, optional
Schema where the CSV file will be ingested.
- table_name: str, optional
The final relation/table name. If unspecified, the name is set to the name of the file or parent directory.
- sep: str, optional
Column separator.
- header: bool, optional
If set to
False
, the parameterheader_names
is used to name the different columns.- header_names: list, optional
list
of the column names.- dtype: dict, optional
dictionary
of the user types. Providing adictionary
can increase ingestion speed and precision; instead of parsing the file to guess the different types, VerticaPy will use the input types.- na_rep: str, optional
Missing values representation.
- quotechar: str, optional
Char that encloses the
str
values.- escape: str, optional
Separator between each record.
- record_terminator: str, optional
A single-character value used to specify the end of a record.
- trim: bool, optional
boolean
, specifies whether to trim white space from header names and key values.- omit_empty_keys: bool, optional
boolean
, specifies how the parser handles header keys without values. IfTrue
, keys with an empty value in the header row are not loaded.- reject_on_duplicate: bool, optional
boolean
, specifies whether to ignore duplicate records (False
), or to reject duplicates (True
). In either case, the load continues.- reject_on_empty_key: bool, optional
boolean
, specifies whether to reject any row containing a key without a value.- reject_on_materialized_type_error: bool, optional
boolean
, specifies whether to reject any materialized column value that the parser cannot coerce into a compatible data type.- parse_nrows: int, optional
If this parameter is greater than zero, a new file of
parse_nrows
rows is created and ingested to identify the data types. It is then dropped and the entire file is ingested. The data types identification will be less precise but this parameter can make the process faster if the file is large.- insert: bool, optional
If set to
True
, the data is ingested into the specified input relation. Be sure that your file has a header corresponding to the name of the relation columns, otherwise ingestion will fail.- temporary_table: bool, optional
If set to
True
, a temporary table will be created.- temporary_local_table: bool, optional
If set to
True
, a temporary local table will be created. The parameterschema
must be empty, otherwise this parameter is ignored.- gen_tmp_table_name: bool, optional
Sets the name of the temporary table. This parameter is only used when the parameter
temporary_local_table
is set toTrue
and if the parameterstable_name
andschema
are unspecified.- ingest_local: bool, optional
If set to
True
, the file is ingested from the local machine.- genSQL: bool, optional
If set to
True
, the SQL code for creating the final table is generated but not executed. This is a good way to change the final relation types or to customize the data ingestion.- materialize: bool, optional
If set to
True
, the flex table is materialized into a table. Otherwise, it will remain a flex table. Flex tables simplify the data ingestion but have worse performace compared to regular tables.
Returns#
- vDataFrame
The
vDataFrame
of the relation.
Examples#
In this example, we will first create a CSV file using
vDataFrame.
to_csv()
and ingest it into Vertica database.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 fromverticapy
are used as intended without interfering with functions from other libraries.We will use the Titanic dataset.
import verticapy.datasets as vpd data = vpd.load_titanic()
123pclassInteger123survivedIntegerAbcVarchar(164)AbcsexVarchar(20)123ageNumeric(8)123sibspInteger123parchIntegerAbcticketVarchar(36)123fareNumeric(12)AbccabinVarchar(30)AbcembarkedVarchar(20)AbcboatVarchar(100)123bodyIntegerAbchome.destVarchar(100)1 1 0 female 2.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 2 1 0 male 30.0 1 2 113781 151.55 C22 C26 S [null] 135 Montreal, PQ / Chesterville, ON 3 1 0 female 25.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 4 1 0 male 39.0 0 0 112050 0.0 A36 S [null] [null] Belfast, NI 5 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 Montevideo, Uruguay 6 1 0 male 47.0 1 0 PC 17757 227.525 C62 C64 C [null] 124 New York, NY 7 1 0 male [null] 0 0 PC 17318 25.925 [null] S [null] [null] New York, NY 8 1 0 male 24.0 0 1 PC 17558 247.5208 B58 B60 C [null] [null] Montreal, PQ 9 1 0 male 36.0 0 0 13050 75.2417 C6 C A [null] Winnipeg, MN 10 1 0 male 25.0 0 0 13905 26.0 [null] C [null] 148 San Francisco, CA 11 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] Trenton, NJ 12 1 0 male 42.0 0 0 110489 26.55 D22 S [null] [null] London / Winnipeg, MB 13 1 0 male 41.0 0 0 113054 30.5 A21 S [null] [null] Pomeroy, WA 14 1 0 male 48.0 0 0 PC 17591 50.4958 B10 C [null] 208 Omaha, NE 15 1 0 male [null] 0 0 112379 39.6 [null] C [null] [null] Philadelphia, PA 16 1 0 male 45.0 0 0 113050 26.55 B38 S [null] [null] Washington, DC 17 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] [null] 18 1 0 male 33.0 0 0 695 5.0 B51 B53 B55 S [null] [null] New York, NY 19 1 0 male 28.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay 20 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay 21 1 0 male 49.0 0 0 19924 26.0 [null] S [null] [null] Ascot, Berkshire / Rochester, NY 22 1 0 male 36.0 1 0 19877 78.85 C46 S [null] 172 Little Onn Hall, Staffs 23 1 0 male 46.0 1 0 W.E.P. 5734 61.175 E31 S [null] [null] Amenia, ND 24 1 0 male [null] 0 0 112051 0.0 [null] S [null] [null] Liverpool, England / Belfast 25 1 0 male 27.0 1 0 13508 136.7792 C89 C [null] [null] Los Angeles, CA 26 1 0 male [null] 0 0 110465 52.0 A14 S [null] [null] Stoughton, MA 27 1 0 male 47.0 0 0 5727 25.5875 E58 S [null] [null] Victoria, BC 28 1 0 male 37.0 1 1 PC 17756 83.1583 E52 C [null] [null] Lakewood, NJ 29 1 0 male [null] 0 0 113791 26.55 [null] S [null] [null] Roachdale, IN 30 1 0 male 70.0 1 1 WE/P 5735 71.0 B22 S [null] 269 Milwaukee, WI 31 1 0 male 39.0 1 0 PC 17599 71.2833 C85 C [null] [null] New York, NY 32 1 0 male 31.0 1 0 F.C. 12750 52.0 B71 S [null] [null] Montreal, PQ 33 1 0 male 50.0 1 0 PC 17761 106.425 C86 C [null] 62 Deephaven, MN / Cedar Rapids, IA 34 1 0 male 39.0 0 0 PC 17580 29.7 A18 C [null] 133 Philadelphia, PA 35 1 0 female 36.0 0 0 PC 17531 31.6792 A29 C [null] [null] New York, NY 36 1 0 male [null] 0 0 PC 17483 221.7792 C95 S [null] [null] [null] 37 1 0 male 30.0 0 0 113051 27.75 C111 C [null] [null] New York, NY 38 1 0 male 19.0 3 2 19950 263.0 C23 C25 C27 S [null] [null] Winnipeg, MB 39 1 0 male 64.0 1 4 19950 263.0 C23 C25 C27 S [null] [null] Winnipeg, MB 40 1 0 male [null] 0 0 113778 26.55 D34 S [null] [null] Westcliff-on-Sea, Essex 41 1 0 male [null] 0 0 112058 0.0 B102 S [null] [null] [null] 42 1 0 male 37.0 1 0 113803 53.1 C123 S [null] [null] Scituate, MA 43 1 0 male 47.0 0 0 111320 38.5 E63 S [null] 275 St Anne's-on-Sea, Lancashire 44 1 0 male 24.0 0 0 PC 17593 79.2 B86 C [null] [null] [null] 45 1 0 male 71.0 0 0 PC 17754 34.6542 A5 C [null] [null] New York, NY 46 1 0 male 38.0 0 1 PC 17582 153.4625 C91 S [null] 147 Winnipeg, MB 47 1 0 male 46.0 0 0 PC 17593 79.2 B82 B84 C [null] [null] New York, NY 48 1 0 male [null] 0 0 113796 42.4 [null] S [null] [null] [null] 49 1 0 male 45.0 1 0 36973 83.475 C83 S [null] [null] New York, NY 50 1 0 male 40.0 0 0 112059 0.0 B94 S [null] 110 [null] 51 1 0 male 55.0 1 1 12749 93.5 B69 S [null] 307 Montreal, PQ 52 1 0 male 42.0 0 0 113038 42.5 B11 S [null] [null] London / Middlesex 53 1 0 male [null] 0 0 17463 51.8625 E46 S [null] [null] Brighton, MA 54 1 0 male 55.0 0 0 680 50.0 C39 S [null] [null] London / Birmingham 55 1 0 male 42.0 1 0 113789 52.0 [null] S [null] 38 New York, NY 56 1 0 male [null] 0 0 PC 17600 30.6958 [null] C 14 [null] New York, NY 57 1 0 female 50.0 0 0 PC 17595 28.7125 C49 C [null] [null] Paris, France New York, NY 58 1 0 male 46.0 0 0 694 26.0 [null] S [null] 80 Bennington, VT 59 1 0 male 50.0 0 0 113044 26.0 E60 S [null] [null] London 60 1 0 male 32.5 0 0 113503 211.5 C132 C [null] 45 [null] 61 1 0 male 58.0 0 0 11771 29.7 B37 C [null] 258 Buffalo, NY 62 1 0 male 41.0 1 0 17464 51.8625 D21 S [null] [null] Southington / Noank, CT 63 1 0 male [null] 0 0 113028 26.55 C124 S [null] [null] Portland, OR 64 1 0 male [null] 0 0 PC 17612 27.7208 [null] C [null] [null] Chicago, IL 65 1 0 male 29.0 0 0 113501 30.0 D6 S [null] 126 Springfield, MA 66 1 0 male 30.0 0 0 113801 45.5 [null] S [null] [null] London / New York, NY 67 1 0 male 30.0 0 0 110469 26.0 C106 S [null] [null] Brockton, MA 68 1 0 male 19.0 1 0 113773 53.1 D30 S [null] [null] New York, NY 69 1 0 male 46.0 0 0 13050 75.2417 C6 C [null] 292 Vancouver, BC 70 1 0 male 54.0 0 0 17463 51.8625 E46 S [null] 175 Dorchester, MA 71 1 0 male 28.0 1 0 PC 17604 82.1708 [null] C [null] [null] New York, NY 72 1 0 male 65.0 0 0 13509 26.55 E38 S [null] 249 East Bridgewater, MA 73 1 0 male 44.0 2 0 19928 90.0 C78 Q [null] 230 Fond du Lac, WI 74 1 0 male 55.0 0 0 113787 30.5 C30 S [null] [null] Montreal, PQ 75 1 0 male 47.0 0 0 113796 42.4 [null] S [null] [null] Washington, DC 76 1 0 male 37.0 0 1 PC 17596 29.7 C118 C [null] [null] Brooklyn, NY 77 1 0 male 58.0 0 2 35273 113.275 D48 C [null] 122 Lexington, MA 78 1 0 male 64.0 0 0 693 26.0 [null] S [null] 263 Isle of Wight, England 79 1 0 male 65.0 0 1 113509 61.9792 B30 C [null] 234 Providence, RI 80 1 0 male 28.5 0 0 PC 17562 27.7208 D43 C [null] 189 ?Havana, Cuba 81 1 0 male [null] 0 0 112052 0.0 [null] S [null] [null] Belfast 82 1 0 male 45.5 0 0 113043 28.5 C124 S [null] 166 Surbiton Hill, Surrey 83 1 0 male 23.0 0 0 12749 93.5 B24 S [null] [null] Montreal, PQ 84 1 0 male 29.0 1 0 113776 66.6 C2 S [null] [null] Isleworth, England 85 1 0 male 18.0 1 0 PC 17758 108.9 C65 C [null] [null] Madrid, Spain 86 1 0 male 47.0 0 0 110465 52.0 C110 S [null] 207 Worcester, MA 87 1 0 male 38.0 0 0 19972 0.0 [null] S [null] [null] Rotterdam, Netherlands 88 1 0 male 22.0 0 0 PC 17760 135.6333 [null] C [null] 232 [null] 89 1 0 male [null] 0 0 PC 17757 227.525 [null] C [null] [null] [null] 90 1 0 male 31.0 0 0 PC 17590 50.4958 A24 S [null] [null] Trenton, NJ 91 1 0 male [null] 0 0 113767 50.0 A32 S [null] [null] Seattle, WA 92 1 0 male 36.0 0 0 13049 40.125 A10 C [null] [null] Winnipeg, MB 93 1 0 male 55.0 1 0 PC 17603 59.4 [null] C [null] [null] New York, NY 94 1 0 male 33.0 0 0 113790 26.55 [null] S [null] 109 London 95 1 0 male 61.0 1 3 PC 17608 262.375 B57 B59 B63 B66 C [null] [null] Haverford, PA / Cooperstown, NY 96 1 0 male 50.0 1 0 13507 55.9 E44 S [null] [null] Duluth, MN 97 1 0 male 56.0 0 0 113792 26.55 [null] S [null] [null] New York, NY 98 1 0 male 56.0 0 0 17764 30.6958 A7 C [null] [null] St James, Long Island, NY 99 1 0 male 24.0 1 0 13695 60.0 C31 S [null] [null] Huntington, WV 100 1 0 male [null] 0 0 113056 26.0 A19 S [null] [null] Streatham, Surrey 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 convert the
vDataFrame
to a CSV.data[0:20].to_csv( path = "titanic_subset.csv", )
Our CSV file is ready to be ingested in database.
Let’s generate, the SQL needed to create the Table.
from verticapy.core.parsers.csv import read_csv read_csv( path = "titanic_subset.csv", table_name = "titanic_subset", schema = "public", quotechar = '"', sep = ",", na_rep = "", genSQL = True, ) Out[4]: ['CREATE TABLE "public"."titanic_subset"("pclass" Integer, "survived" Integer, "name" Varchar(94), "sex" Varchar(20), "age" Numeric(8,4), "sibsp" Integer, "parch" Integer, "ticket" Varchar(20), "fare" Numeric(11,6), "cabin" Varchar(22), "embarked" Varchar(20), "boat" Varchar(20), "body" Integer, "home.dest" Varchar(62))', 'COPY "public"."titanic_subset"("pclass", "survived", "name", "sex", "age", "sibsp", "parch", "ticket", "fare", "cabin", "embarked", "boat", "body", "home.dest") FROM LOCAL \'titanic_subset.csv\' UNCOMPRESSED DELIMITER \',\' NULL \'\' ENCLOSED BY \'"\' RECORD TERMINATOR E\'\\n\' ESCAPE AS \'\x17\' SKIP 1']
Note
When
genSQL
flag is set toTrue
, the SQL code for creating the final table is generated but not executed. This is a good way to change the final relation types or to customize the data ingestion.Now, we will ingest the CSV file into the Vertica database.
read_csv( path = "titanic_subset.csv", table_name = "titanic_subset", schema = "public", quotechar = '"', sep = ",", na_rep = "", )
123pclassInteger123survivedIntegerAbcVarchar(94)AbcsexVarchar(20)123ageNumeric(10)123sibspInteger123parchIntegerAbcticketVarchar(20)123fareNumeric(13)AbccabinVarchar(22)AbcembarkedVarchar(20)AbcboatVarchar(20)123bodyIntegerAbchome.destVarchar(62)1 1 0 female 2.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 2 1 0 male 30.0 1 2 113781 151.55 C22 C26 S [null] 135 Montreal, PQ / Chesterville, ON 3 1 0 female 25.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 4 1 0 male 39.0 0 0 112050 0.0 A36 S [null] [null] Belfast, NI 5 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 Montevideo, Uruguay 6 1 0 male 47.0 1 0 PC 17757 227.525 C62 C64 C [null] 124 New York, NY 7 1 0 male [null] 0 0 PC 17318 25.925 [null] S [null] [null] New York, NY 8 1 0 male 24.0 0 1 PC 17558 247.5208 B58 B60 C [null] [null] Montreal, PQ 9 1 0 male 36.0 0 0 13050 75.2417 C6 C A [null] Winnipeg, MN 10 1 0 male 25.0 0 0 13905 26.0 [null] C [null] 148 San Francisco, CA 11 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] Trenton, NJ 12 1 0 male 42.0 0 0 110489 26.55 D22 S [null] [null] London / Winnipeg, MB 13 1 0 male 41.0 0 0 113054 30.5 A21 S [null] [null] Pomeroy, WA 14 1 0 male 48.0 0 0 PC 17591 50.4958 B10 C [null] 208 Omaha, NE 15 1 0 male [null] 0 0 112379 39.6 [null] C [null] [null] Philadelphia, PA 16 1 0 male 45.0 0 0 113050 26.55 B38 S [null] [null] Washington, DC 17 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] [null] 18 1 0 male 33.0 0 0 695 5.0 B51 B53 B55 S [null] [null] New York, NY 19 1 0 male 28.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay 20 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay Rows: 1-20 | Columns: 14Important
A limited number of rows, determined by the
parse_nrows
parameter, is ingested. If your dataset is large and you want to ingest the entire dataset, increase its value.Let’s specify data types using
dtype
parameter.read_csv( path = "titanic_subset.csv", table_name = "titanic_sub_dtypes", schema = "public", quotechar = '"', sep = ",", na_rep = "", dtype = { "pclass": "Integer", "survived": "Integer", "name": "Varchar(164)", "sex": "Varchar(20)", "age": "Numeric(6,3)", "sibsp": "Integer", "parch": "Integer", "ticket": "Varchar(36)", "fare": "Numeric(10,5)", "cabin": "Varchar(30)", "embarked": "Varchar(20)", "boat": "Varchar(100)", "body": "Integer", "home.dest": "Varchar(100)", }, )
123pclassInteger123survivedIntegerAbcVarchar(164)AbcsexVarchar(20)123ageNumeric(8)123sibspInteger123parchIntegerAbcticketVarchar(36)123fareNumeric(12)AbccabinVarchar(30)AbcembarkedVarchar(20)AbcboatVarchar(100)123bodyIntegerAbchome.destVarchar(100)1 1 0 female 2.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 2 1 0 male 30.0 1 2 113781 151.55 C22 C26 S [null] 135 Montreal, PQ / Chesterville, ON 3 1 0 female 25.0 1 2 113781 151.55 C22 C26 S [null] [null] Montreal, PQ / Chesterville, ON 4 1 0 male 39.0 0 0 112050 0.0 A36 S [null] [null] Belfast, NI 5 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 Montevideo, Uruguay 6 1 0 male 47.0 1 0 PC 17757 227.525 C62 C64 C [null] 124 New York, NY 7 1 0 male [null] 0 0 PC 17318 25.925 [null] S [null] [null] New York, NY 8 1 0 male 24.0 0 1 PC 17558 247.5208 B58 B60 C [null] [null] Montreal, PQ 9 1 0 male 36.0 0 0 13050 75.2417 C6 C A [null] Winnipeg, MN 10 1 0 male 25.0 0 0 13905 26.0 [null] C [null] 148 San Francisco, CA 11 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] Trenton, NJ 12 1 0 male 42.0 0 0 110489 26.55 D22 S [null] [null] London / Winnipeg, MB 13 1 0 male 41.0 0 0 113054 30.5 A21 S [null] [null] Pomeroy, WA 14 1 0 male 48.0 0 0 PC 17591 50.4958 B10 C [null] 208 Omaha, NE 15 1 0 male [null] 0 0 112379 39.6 [null] C [null] [null] Philadelphia, PA 16 1 0 male 45.0 0 0 113050 26.55 B38 S [null] [null] Washington, DC 17 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] [null] 18 1 0 male 33.0 0 0 695 5.0 B51 B53 B55 S [null] [null] New York, NY 19 1 0 male 28.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay 20 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] Montevideo, Uruguay Rows: 1-20 | Columns: 14Note
You can ingest multiple CSV files into the Vertica database by using the following syntax.
read_csv( path = "*.csv", table_name = "titanic_multi_files", schema = "public", quotechar = '"', sep = ",", na_rep = "", )
Note
The
read_csv()
function offers various additional parameters and options. Check the documentation to explore its capabilities, such as the ability to automatically guess the separator or ingest a specific number of rows.See also
read_json()
: Ingests a JSON file into the Vertica DB.