Wolfram Research

Function Repository Resource:

ImportCSVToDataset

Source Notebook

Import CSV and TSV files to datasets

Contributed by: Anton Antonov

ResourceFunction["ImportCSVToDataset"][fname]

imports the CSV or TSV file fname into a dataset.

ResourceFunction["ImportCSVToDataset"][fname,frm]

imports the file fname into a dataset assuming the format frm.

Details and Options

ResourceFunction["ImportCSVToDataset"] takes all options of Import.
Here are the options and default values:
"ColumnNames" True whether to treat the first row as column names
"RowNames" False whether to treat the first column as row names

Examples

Basic Examples

Here a CSV file from GitHub is imported:

In[1]:=
ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv"]
Out[1]=

Options

ColumnNames

Treat the first row and column of the file as ordinary data:

In[2]:=
ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "ColumnNames" -> False]
Out[2]=

RowNames

Treat both the first row and column of the array as names:

In[3]:=
ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "RowNames" -> True]
Out[3]=

Properties and Relations

ImportCSVToDataset will give similar results to Import with "Dataset" as the specified result type:

In[4]:=
Import["https://raw.githubusercontent.com/antononcube/MathematicaVsR/\
master/Data/MathematicaVsR-Data-Titanic.csv", "Dataset", "HeaderLines" -> 1]
Out[4]=

A dataset exported as CSV can be imported back to a dataset:

In[5]:=
titanicDS = ResourceFunction["ImportCSVToDataset"][
   "https://raw.githubusercontent.com/antononcube/MathematicaVsR/\
master/Data/MathematicaVsR-Data-Titanic.csv", "RowNames" -> True];

Export the dataset to a temporary location as a CSV file:

In[6]:=
exportedCSV = Export["/tmp/titanic.csv", titanicDS, "CSV"];

Use ImportCSVToDataset to bring it back to a dataset:

In[7]:=
ResourceFunction["ImportCSVToDataset"][exportedCSV, "RowNames" -> True]
Out[7]=

Possible Issues

Importing CSV files as TSV produces a one-column dataset:

In[8]:=
ResourceFunction[
 "ImportCSVToDataset"]["https://raw.githubusercontent.com/antononcube/\
MathematicaVsR/master/Data/MathematicaVsR-Data-Titanic.csv", "TSV"]
Out[8]=

Resource History

Related Resources

License Information