Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Import CSV and TSV files to datasets
| ResourceFunction["ImportCSVToDataset"][fname,"format"] imports the file fname into a Dataset, assuming the file is in the specified format. | 
| "ColumnNames" | True | whether to treat the first row as column names | 
| "RowNames" | False | whether to treat the first column as row names | 
| CharacterEncoding | "UTF8ISOLatin1" | raw character encoding used in the file | 
| "CurrencyTokens" | {{"$", "£", "¥", "€"}, {"c", "¢", "p", "F"}} | currency units to be skipped when importing numerical values | 
| "DateStringFormat" | None | date format, given as a DateString specification | 
| "FieldSeparators" | "," | field separator | 
| "FillRows" | Automatic | whether to fill rows to the max column length | 
| "HeaderLines" | 0 | number of lines to assume as headers | 
| "IgnoreEmptyLines" | False | whether to ignore empty lines | 
| Method | Import | importing method to use | 
| "NumberPoint" | "." | decimal point string | 
| "Numeric" | Automatic | whether to import data fields as numbers if possible | 
| "SkipLines" | 0 | number of lines to skip at the beginning of the file | 
Here, a CSV file from GitHub is imported:
| In[1]:= | ![ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv"]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/7927adb4909eb577.png)  | 
| Out[1]= |   | 
If the first argument is a CSV string (not a file name or URL), then that string can be imported by specifying ImportString as the importing method:
| In[2]:= | ![tbl = "V1,V2
1,1
2,2
3,3";
ResourceFunction["ImportCSVToDataset"][tbl, "CSV", Method -> ImportString]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/2270a34e53f11a62.png)  | 
| Out[3]= |   | 
Treat the first row and column of the file as ordinary data:
| In[4]:= | ![ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "ColumnNames" -> False]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/545c7d61b75c3185.png)  | 
| Out[4]= |   | 
Treat both the first row and column of the array as names:
| In[5]:= | ![ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "RowNames" -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/6da8dca99f71f126.png)  | 
| Out[5]= |   | 
CSV files can use field separators other than commas, such as ";":
| In[6]:= |   | 
Import using the default field separator:
| In[7]:= | ![ResourceFunction["ImportCSVToDataset"][tbl, "CSV", Method -> ImportString]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/6323925e42d48ea1.png)  | 
| Out[7]= |   | 
Importing using a proper field separator:
| In[8]:= | ![ResourceFunction["ImportCSVToDataset"][tbl, Method -> ImportString, "FieldSeparators" -> ";"]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/1a56cb08399bfc3c.png)  | 
| Out[8]= |   | 
ImportCSVToDataset will give similar results to Import with "Dataset" specified as the result type:
| In[9]:= | ![Import["https://raw.githubusercontent.com/antononcube/MathematicaVsR/\
master/Data/MathematicaVsR-Data-Titanic.csv", "Dataset", "HeaderLines" -> 1]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/4808ae7a59d41fd4.png)  | 
| Out[9]= |   | 
A dataset exported as CSV can be imported back to a dataset:
| In[10]:= | ![titanicDS = ResourceFunction["ImportCSVToDataset"][
   "https://raw.githubusercontent.com/antononcube/MathematicaVsR/\
master/Data/MathematicaVsR-Data-Titanic.csv", "RowNames" -> True];](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/7bafc79c191772dc.png)  | 
Export the dataset to a temporary location as a CSV file:
| In[11]:= | ![exportedCSV = Export["/tmp/titanic.csv", titanicDS, "CSV"];](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/7ff6eccda0e47f85.png)  | 
Use ImportCSVToDataset to bring it back to a dataset:
| In[12]:= | ![ResourceFunction["ImportCSVToDataset"][exportedCSV, "RowNames" -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/7b7502fe751586c9.png)  | 
| Out[12]= |   | 
Importing CSV files as TSV produces a one-column dataset:
| In[13]:= | ![ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "TSV"]](https://www.wolframcloud.com/obj/resourcesystem/images/d29/d29f97d9-cd9b-48b4-b6e0-1beccdd1f128/45cad87c08866666.png)  | 
| Out[13]= |   | 
This work is licensed under a Creative Commons Attribution 4.0 International License