The CSV magician
xan, the CSV magicianxan is a command line tool that can be used to process CSV files directly from the shell.
It has been written in Rust to be as fast as possible, use as little memory as possible, and can very easily handle large CSV files (gigabytes to terabytes). It leverages a novel SIMD CSV parser and is also able to parallelize some computations (through multithreading) to make some tasks complete as fast as your hardware will allow.
It can easily preview, filter, slice, aggregate, sort, join CSV files, and exposes a large collection of composable commands that can be chained together to perform a wide variety of typical tabular data processing tasks.
xan also offers its own expression language so you can perform complex tasks that cannot be done by relying on the simplest commands. This minimalistic language has been tailored for CSV data and is way faster than evaluating typical script languages such as Python, Lua, JavaScript etc.
Note that this tool is originally a fork of BurntSushi's xsv, but has been nearly entirely rewritten at that point, to fit SciencesPo's médialab use-cases, rooted in web data collection and analysis geared towards social sciences (you might think CSV is outdated by now, but read our love letter to the format before judging too quickly).
xan therefore goes beyond typical data manipulation and expose utilities related to lexicometry, graph theory and even scraping.
Beyond CSV data, xan is able to process a large variety of CSV-adjacent data formats from many different disciplines such as web archival (.cdx) or bioinformatics (.vcf, .gtf, .sam, .bed etc.). xan is also able to convert to & from many data formats such as json, excel files, numpy arrays etc. using xan to and xan from. See this section for more detail.
Then, even though xan is fundamentally geared towards streams of row-oriented tabular data, it can sometimes leverage the benefits of the popular parquet file format to offer better performance. See this section for more detail.
Finally, xan can be used to display CSV files in the terminal, for easy exploration, and can even be used to draw basic data visualisations:
| view command | flatten command |
|---|---|
| categorical histogram | scatterplot |
| categorical scatterplot | histograms |
| parallel processing | time series |
| small multiples (facet grid) | grouped view |
| correlation matrix heatmap | heatmap |
xan can be installed using cargo (it usually comes with Rust):
cargo install xan --lockedYou can also tweak the build flags to make sure the Rust compiler is able to leverage all your CPU's features:
CARGO_BUILD_RUSTFLAGS='-C target-cpu=native' cargo install xan --lockedYou can also install the latest dev version thusly:
cargo install --git https://github.com/medialab/xan --lockedxan can be installed using Scoop on Windows:
scoop bucket add extras
scoop install xanxan can be installed with Homebrew on macOS thusly:
brew install xanYou can install xan from the extra repository using pacman:
sudo pacman -S xanA package is available from the official repositories. To install xan simply run:
pkgin install xanxan is packaged for Nix, and is available in Nixpkgs as of 25.05 release. To
install it, you may add it to your environment.systemPackages as pkgs.xan or
use nix-shell to enter an ephemeral shell.
nix-shell -p xanxan can be installed in Linux, macOS, and Windows using the Pixi package manager:
pixi global install xanxan can be installed through conda-forge thusly:
conda install conda-forge::xanPre-built binaries can be found attached to every GitHub releases.
Currently supported targets include:
x86_64-apple-darwin
x86_64-unknown-linux-gnu
x86_64-unknown-linux-musl
x86_64-pc-windows-msvc
aarch64-apple-darwin
aarch64-unknown-linux-gnu
ppc64le targets are not built by the CI yet but prebuilt binaries can still be found in the conda-forge package's files if you need them.
Feel free to open a PR to improve the CI by adding relevant targets.
Note that xan also exposes handy automatic completions for command and header/column names that you can install through the xan completions command.
Run the following command to understand how to install those completions:
xan completions -hFor zsh, you can add the completion file to ~/.zfunc:
mkdir -p ~/.zfunc
xan completions zsh > ~/.zfunc/_xanThen add this before compinit in your ~/.zshrc:
fpath=(~/.zfunc $fpath)
autoload -Uz compinit
compinitLet's learn about the most commonly used xan commands by exploring a corpus of French medias:
curl -LO https://github.com/medialab/corpora/raw/master/polarisation/medias.csvxan headers medias.csv0 webentity_id
1 name
2 prefixes
3 home_page
4 start_pages
5 indegree
6 hyphe_creation_timestamp
7 hyphe_last_modification_timestamp
8 outreach
9 foundation_year
10 batch
11 edito
12 parody
13 origin
14 digital_native
15 mediacloud_ids
16 wheel_category
17 wheel_subcategory
18 has_paywall
19 inactivexan count medias.csv478xan view medias.csv…On unix, don't hesitate to use the -p flag to automagically forward the full output to an appropriate pager and skim through all the columns.
# NOTE: drop -c to avoid truncating the values
xan flatten -c medias.csv…xan search -s outreach internationale medias.csv | xan view -s name,outreach…xan select foundation_year,name medias.csv | xan view…xan sort -s foundation_year medias.csv | xan view -s name,foundation_year…# Some medias of our corpus have the same ids on mediacloud.org
xan dedup -s mediacloud_ids medias.csv | xan count && xan count medias.csv457
478Deduplicating can also be done while sorting:
xan sort -s mediacloud_ids -u medias.csvxan frequency -s edito medias.csv | xan viewDisplaying 3 cols from 5 rows of <stdin>
┌───┬───────┬────────────┬───────┐
│ - │ field │ value │ count │
├───┼───────┼────────────┼───────┤
│ 0 │ edito │ media │ 423 │
│ 1 │ edito │ individu │ 30 │
│ 2 │ edito │ plateforme │ 14 │
│ 3 │ edito │ agrégateur │ 10 │
│ 4 │ edito │ agence │ 1 │
└───┴───────┴────────────┴───────┘xan frequency -s edito medias.csv | xan histHistogram for edito (bars: 5, sum: 478, max: 423):
media |423 88.49%|━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━|
individu | 30 6.28%|━━━╸ |
plateforme | 14 2.93%|━╸ |
agrégateur | 10 2.09%|━╸ |
agence | 1 0.21%|╸ |xan stats -s indegree,edito medias.csv | xan transpose | xan view -I…xan filter 'batch > 1' medias.csv | xan count130Note that the expression language distinguishes between arithmetic & string operators. Filtering on the media column which contains strings, for instance, you would rather use the eq operator than the == one that should be used for numbers:
xan filter 'edito eq "plateforme"'This said, for this particular use-case you should often stick to xan search instead.
To access the expression language's cheatsheet, run xan help cheatsheet. To display the full list of available functions, run xan help functions.
xan map 'fmt("{} ({})", name, foundation_year) as key' medias.csv | xan select key | xan slice -l 10key
Acrimed.org (2002)
24matins.fr (2006)
Actumag.info (2013)
2012un-Nouveau-Paradigme.com (2012)
24heuresactu.com (2010)
AgoraVox (2005)
Al-Kanz.org (2008)
Alalumieredunouveaumonde.blogspot.com (2012)
Allodocteurs.fr (2005)
Alterinfo.net (2005)To access the expression language's cheatsheet, run xan help cheatsheet. To display the full list of available functions, run xan help functions.
xan transform name 'split(name, ".") | first | upper' medias.csv | xan select name | xan slice -l 10name
ACRIMED
24MATINS
ACTUMAG
2012UN-NOUVEAU-PARADIGME
24HEURESACTU
AGORAVOX
AL-KANZ
ALALUMIEREDUNOUVEAUMONDE
ALLODOCTEURS
ALTERINFOTo access the expression language's cheatsheet, run xan help cheatsheet. To display the full list of available functions, run xan help functions.
xan agg 'sum(indegree) as total_indegree, mean(indegree) as mean_indegree' medias.csv | xan view -IDisplaying 1 col from 1 rows of <stdin>
┌────────────────┬───────────────────┐
│ total_indegree │ mean_indegree │
├────────────────┼───────────────────┤
│ 25987 │ 56.12742980561554 │
└─────────No open issues yet, or sync has not completed.