Importing Data and Dataset
Prerequisites: Associations
Import and Export
Import reads hundreds of formats — CSV, TSV, Excel, JSON, XML, images,
audio — and Export writes them. The format comes from the file
extension or a second argument.
ImportString["name,age\nAda,36\nAlan,41", "CSV"]
ImportString["{\"a\": 1, \"b\": [2, 3]}", "RawJSON"]
ExportString[<|"x" -> 1, "y" -> {1, 2}|>, "RawJSON", "Compact" -> True]
{{name, age}, {Ada, 36}, {Alan, 41}}
<|a -> 1, b -> {2, 3}|>
{"x":1,"y":[1,2]}
ImportStringandExportStringwork on text instead of files — handy for testing.Import["file.csv"]andExport["file.json", data]work the same way with files.- Numbers in CSV become numbers automatically.
"RawJSON"maps JSON objects to associations. (Check JSON with the JSON Formatter if an import fails.)
Dataset
A Dataset wraps a list of associations (or other nested data) with a table display and a query language.
ds = Dataset[{
<|"name" -> "Ada", "dept" -> "IT", "salary" -> 61000|>,
<|"name" -> "Bo", "dept" -> "Sales", "salary" -> 52000|>,
<|"name" -> "Cy", "dept" -> "IT", "salary" -> 70000|>}];
In a notebook, ds displays as a table with the columns name, dept, and
salary. For files, Import["staff.csv", "Dataset", HeaderLines -> 1]
builds the same structure from a CSV with a header row.
Querying
A query is written as ds[rows, columns] — each argument is an
operation applied at that level:
ds[Total, "salary"]
ds[Mean, "salary"]
ds[Select[#salary > 55000 &], "name"] // Normal
ds[SortBy[-#salary &], "name"] // Normal
ds[GroupBy["dept"], Total, "salary"] // Normal
ds[Counts, "dept"] // Normal
183000
61000
{Ada, Cy}
{Cy, Ada, Bo}
<|IT -> 131000, Sales -> 52000|>
<|IT -> 2, Sales -> 1|>
ds[Total, "salary"]— take the"salary"column, then total it.Select[...]andSortBy[...]are operator forms: functions waiting for their data.GroupBy["dept"]splits the rows by department; the next arguments apply within each group.- Queries that return tables come back as Datasets;
Normalconverts them to ordinary lists and associations (used here so the results can be shown as text).
Adding columns
ds[All, <|#, "monthly" -> #salary/12|> &][All, "monthly"] // Normal
{15250/3, 13000/3, 17500/3}
<|#, "monthly" -> ...|> copies each row's association and adds a key. Exact
division gives fractions; use Round or N for readable numbers:
ds[All, Round[#salary/12] &] // Normal
{5083, 4333, 5833}
Beyond CSV
SemanticImportdetects dates, quantities, and entities in text data.Importreads Excel sheets ("XLSX"), andDatabaseReferenceandSQLSelectconnect to SQL databases.Tabular(in recent versions) is a fast, column-oriented table type for large data.