70

I am using the \csvautotabular function to import data from a csv file. It has worked beautifully; however, I only want to import certain columns from the file. How can I exclude certain columns?

EDIT:

Okay, here is my csv file:

Time (s),Zeroed time (s),Y Position in pixels,Zeroed Y Position in pixels,Y Position in meters using original trans. Eq.,Y Position In Meters using new trans. Eq.
43.97,0,734,528,14.22624,18.26294
44.01,0.04,731,525,14.11335,18.14345
44.04,0.07,729,523,14.03819,18.06389
44.07,0.1,726,520,13.9256,17.9447
44.11,0.14,720,514,13.70096,17.70686
44.14,0.17,718,512,13.62624,17.62774
44.17,0.2,714,508,13.47704,17.46974
44.21,0.24,711,505,13.36535,17.35145
44.24,0.27,706,500,13.1796,17.1547
44.27,0.3,700,494,12.95736,16.91926
44.31,0.34,696,490,12.8096,16.7627
44.34,0.37,690,484,12.58856,16.52846
44.37,0.4,685,479,12.40491,16.33381
44.41,0.44,679,473,12.18519,16.10089
44.44,0.47,672,466,11.92976,15.83006
44.47,0.5,664,458,11.63904,15.52174
44.51,0.54,659,453,11.45799,15.32969
44.54,0.57,651,445,11.16935,15.02345
44.57,0.6,642,436,10.84616,14.68046
44.61,0.64,633,427,10.52459,14.33909
44.64,0.67,625,419,10.24011,14.03701
44.67,0.7,616,410,9.9216,13.6987
44.71,0.74,607,401,9.60471,13.36201
44.74,0.77,597,391,9.25451,12.98981
44.77,0.8,587,381,8.90631,12.61961
44.81,0.84,577,371,8.56011,12.25141
44.84,0.87,567,361,8.21591,11.88521
44.87,0.9,554,348,7.77144,11.41214
44.91,0.94,541,335,7.33035,10.94245
44.94,0.97,528,322,6.89264,10.47614
44.97,1,515,309,6.45831,10.01321
45.01,1.04,503,297,6.06039,9.58889
45.04,1.07,490,284,5.63256,9.13246
45.07,1.1,475,269,5.14311,8.61001
45.11,1.14,458,252,4.59384,8.02334
45.14,1.17,444,238,4.14584,7.54454
45.17,1.2,427,221,3.60711,6.96841
45.21,1.24,410,204,3.07416,6.39806
45.24,1.27,393,187,2.54699,5.83349
45.27,1.3,377,171,2.05611,5.30741
45.31,1.34,361,155,1.57035,4.78645
45.34,1.37,346,140,1.1196,4.3027
45.38,1.41,328,122,0.58464,3.72814
45.41,1.44,309,103,0.02699,3.12869
45.44,1.47,289,83,-0.55221,2.50549
45.48,1.51,269,63,-1.12341,1.89029
45.51,1.54,246,40,-1.7704,1.1927
45.54,1.57,224,18,-2.37936,0.53534
45.58,1.61,203,-3,-2.95161,-0.08311
45.61,1.64,206,0,-2.8704,0.0047

The following columns I want to remove: Time (s), Y Position in pixels, Zeroed Y Position in pixels, Y Position in meters using original trans. Eq.

Here is what I have typed in TeXstudio, which doesn't work:

\begin{filecontents*}{Basket_ball.csv}
Time (s),Zeroed time (s),Y Position in pixels,Zeroed Y Position in pixels,Y Position in meters using original trans. Eq.,Y Position In Meters using new trans. Eq.
43.97,0,734,528,14.22624,18.26294
44.01,0.04,731,525,14.11335,18.14345
44.04,0.07,729,523,14.03819,18.06389
44.07,0.1,726,520,13.9256,17.9447
44.11,0.14,720,514,13.70096,17.70686
44.14,0.17,718,512,13.62624,17.62774
44.17,0.2,714,508,13.47704,17.46974
44.21,0.24,711,505,13.36535,17.35145
44.24,0.27,706,500,13.1796,17.1547
44.27,0.3,700,494,12.95736,16.91926
44.31,0.34,696,490,12.8096,16.7627
44.34,0.37,690,484,12.58856,16.52846
44.37,0.4,685,479,12.40491,16.33381
44.41,0.44,679,473,12.18519,16.10089
44.44,0.47,672,466,11.92976,15.83006
44.47,0.5,664,458,11.63904,15.52174
44.51,0.54,659,453,11.45799,15.32969
44.54,0.57,651,445,11.16935,15.02345
44.57,0.6,642,436,10.84616,14.68046
44.61,0.64,633,427,10.52459,14.33909
44.64,0.67,625,419,10.24011,14.03701
44.67,0.7,616,410,9.9216,13.6987
44.71,0.74,607,401,9.60471,13.36201
44.74,0.77,597,391,9.25451,12.98981
44.77,0.8,587,381,8.90631,12.61961
44.81,0.84,577,371,8.56011,12.25141
44.84,0.87,567,361,8.21591,11.88521
44.87,0.9,554,348,7.77144,11.41214
44.91,0.94,541,335,7.33035,10.94245
44.94,0.97,528,322,6.89264,10.47614
44.97,1,515,309,6.45831,10.01321
45.01,1.04,503,297,6.06039,9.58889
45.04,1.07,490,284,5.63256,9.13246
45.07,1.1,475,269,5.14311,8.61001
45.11,1.14,458,252,4.59384,8.02334
45.14,1.17,444,238,4.14584,7.54454
45.17,1.2,427,221,3.60711,6.96841
45.21,1.24,410,204,3.07416,6.39806
45.24,1.27,393,187,2.54699,5.83349
45.27,1.3,377,171,2.05611,5.30741
45.31,1.34,361,155,1.57035,4.78645
45.34,1.37,346,140,1.1196,4.3027
45.38,1.41,328,122,0.58464,3.72814
45.41,1.44,309,103,0.02699,3.12869
45.44,1.47,289,83,-0.55221,2.50549
45.48,1.51,269,63,-1.12341,1.89029
45.51,1.54,246,40,-1.7704,1.1927
45.54,1.57,224,18,-2.37936,0.53534
45.58,1.61,203,-3,-2.95161,-0.08311
45.61,1.64,206,0,-2.8704,0.0047
\end{filecontents*}

\begin{document}

\maketitle

\section*{Theory}


%\csvautotabular{Basket_ball.csv}



\begin{tabular}{| l | r |}\hline%
\bfseries Time (s) & \bfseries Position (m)
\csvreader[head to column names]{Basket_ball.csv}{} % 
{\\\Time (s)\ \Y Position in pixels & \Zeroed Y Position in pixels & \Y Position in meters using original trans. Eq. } % 
\\\hline
\end{tabular}
3

6 Answers 6

79

The csvsimple manual describes it. This is an modified example from the manual.

\documentclass{standalone}

\usepackage{csvsimple}


\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}


\begin{document}
    \begin{tabular}{l|c}%
    \bfseries Person & \bfseries Matr.~No.% specify table head
    \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
    {\\\hline\givenname\ \name & \matriculation}% specify your coloumns here
    \end{tabular}
\end{document}

result is:

result1

If you have spaces in your header you can't use head to column names. So you may use \csvcoli etc. Here a quick example

\documentclass{article}
\usepackage{csvsimple}
    
    
\begin{filecontents*}{Basket_ball.csv}
    Time (s),Zeroed time (s),Y Position in pixels,Zeroed Y Position in pixels,Y Position in meters using original trans. Eq.,Y Position In Meters using new trans. Eq.
    43.97,0,734,528,14.22624,18.26294
    44.01,0.04,731,525,14.11335,18.14345
    44.04,0.07,729,523,14.03819,18.06389
    44.07,0.1,726,520,13.9256,17.9447
    44.11,0.14,720,514,13.70096,17.70686
    44.14,0.17,718,512,13.62624,17.62774
    44.17,0.2,714,508,13.47704,17.46974
    44.21,0.24,711,505,13.36535,17.35145
    44.24,0.27,706,500,13.1796,17.1547
    44.27,0.3,700,494,12.95736,16.91926
    44.31,0.34,696,490,12.8096,16.7627
\end{filecontents*}

\begin{document}



\section*{Theory}
    \begin{tabular}{l|l}%
    \textbf{Time (s)} & \textbf{Zeroed time (s)}% specify table head
    \csvreader[head to column names]{Basket_ball.csv}{}% use head of csv as column names
    {\\\hline\csvcoli&\csvcolii}% specify your coloumns here
    \end{tabular}
\end{document}

result 2

8
  • I am not quite sure I see how this would exclude specific columns from my csv file.
    – Mack
    Nov 24, 2013 at 15:24
  • 2
    @Mack Please take a look at grade.csv. it has a gender and a grade column that isn't showing up at the actual table. The manual also lists different ways that might fit your specific problem in better way.
    – someonr
    Nov 24, 2013 at 15:28
  • I actually just edited my post, which includes my code that I was trying to make similar to the one you gave in your post.
    – Mack
    Nov 24, 2013 at 15:46
  • 3
    After some digging into the manual, I found out that csvsimple is also tsvsimple: you can provide the separator=tab argument (e.g. \csvreader[separator=tab] in the above MWE). Nice! Jun 20, 2020 at 15:40
  • 1
    @Vrangone \bfseries is fine to use 2 days ago
33

You can use pgfplotstable or datatool which I think csvsimple comes from. Here is an example with pgfplotstable with default settings.

\documentclass{standalone}
\usepackage{pgfplotstable,filecontents}
\pgfplotsset{compat=1.9}% supress warning

\begin{filecontents*}{test.csv}
Time (s),Zeroed time (s),Y Position in pixels,Zeroed Y Position in pixels,Y Position in meters using original trans. Eq.,Y Position In Meters using new trans. Eq.
43.97,0,734,528,14.22624,18.26294
44.01,0.04,731,525,14.11335,18.14345
44.04,0.07,729,523,14.03819,18.06389
44.07,0.1,726,520,13.9256,17.9447
44.11,0.14,720,514,13.70096,17.70686
44.14,0.17,718,512,13.62624,17.62774
44.17,0.2,714,508,13.47704,17.46974
44.21,0.24,711,505,13.36535,17.35145
44.24,0.27,706,500,13.1796,17.1547
44.27,0.3,700,494,12.95736,16.91926
44.31,0.34,696,490,12.8096,16.7627
44.34,0.37,690,484,12.58856,16.52846
44.37,0.4,685,479,12.40491,16.33381
44.41,0.44,679,473,12.18519,16.10089
44.44,0.47,672,466,11.92976,15.83006
44.47,0.5,664,458,11.63904,15.52174
44.51,0.54,659,453,11.45799,15.32969
44.54,0.57,651,445,11.16935,15.02345
44.57,0.6,642,436,10.84616,14.68046
44.61,0.64,633,427,10.52459,14.33909
44.64,0.67,625,419,10.24011,14.03701
44.67,0.7,616,410,9.9216,13.6987
44.71,0.74,607,401,9.60471,13.36201
44.74,0.77,597,391,9.25451,12.98981
44.77,0.8,587,381,8.90631,12.61961
44.81,0.84,577,371,8.56011,12.25141
44.84,0.87,567,361,8.21591,11.88521
44.87,0.9,554,348,7.77144,11.41214
44.91,0.94,541,335,7.33035,10.94245
44.94,0.97,528,322,6.89264,10.47614
44.97,1,515,309,6.45831,10.01321
45.01,1.04,503,297,6.06039,9.58889
45.04,1.07,490,284,5.63256,9.13246
45.07,1.1,475,269,5.14311,8.61001
45.11,1.14,458,252,4.59384,8.02334
45.14,1.17,444,238,4.14584,7.54454
45.17,1.2,427,221,3.60711,6.96841
45.21,1.24,410,204,3.07416,6.39806
45.24,1.27,393,187,2.54699,5.83349
45.27,1.3,377,171,2.05611,5.30741
45.31,1.34,361,155,1.57035,4.78645
45.34,1.37,346,140,1.1196,4.3027
45.38,1.41,328,122,0.58464,3.72814
45.41,1.44,309,103,0.02699,3.12869
45.44,1.47,289,83,-0.55221,2.50549
45.48,1.51,269,63,-1.12341,1.89029
45.51,1.54,246,40,-1.7704,1.1927
45.54,1.57,224,18,-2.37936,0.53534
45.58,1.61,203,-3,-2.95161,-0.08311
45.61,1.64,206,0,-2.8704,0.0047
\end{filecontents*}



\begin{document}
\pgfplotstabletypeset[col sep=comma,
     columns={Zeroed time (s),Y Position In Meters using new trans. Eq.},
    ]{test.csv}
\end{document}

enter image description here

3
  • 3
    I have to admit, that this solution is cleaner.
    – someonr
    Nov 24, 2013 at 16:16
  • @someonr I like your honesty, I agree. Thank you guys +1!
    – hhh
    Nov 10, 2014 at 1:55
  • 2
    Is it true that \pgfplotstabletypeset is limited by default to data containing floats only? You should probably add [string type ...] option in order to process csv which contains strings as well.
    – Bach
    Jan 1, 2016 at 11:00
13

The service http://www.tablesgenerator.com/ lets you create latex-based tables in a GUI and then export the code. It also lets you upload a CSV and converts it aut

2
  • 1
    I checked now (2019) and could not find an option for upload the csv file.
    – user855443
    May 11, 2019 at 17:22
  • 1
    @user855443 it's at file -> import csv Oct 19, 2020 at 13:55
6

An alternative option is to programmatically modify the csv file to produce a LaTeX table with the shell code below. This gives you total control over the formatting of the final table and does not require extra packages. The caveat is that some of the substitutions need to be done carefully to avoid altering the content of the table. In general I find that tsv is more resilient to this kind of issues, since table fields rarely contain \t but may contain ,.

#!/bin/sh
# Convert table to latex
cat table.csv | sed \
    -e 's:&:\\&:g' \
    -e 's:,:\t\&\t:g' \
    -e 's:"::g' \
    -e 's/_/\\_/g'  \
    -e 's:$:\t\\\\:g' > table.tex

# Latex table syntax
ed table.tex << END
1i
\begin{tabular}{ll}
\toprule
Header 1 & Header 2  \\\\
\midrule
.
\$a
\hline
\end{tabular}
.
wq
END

Note that the actual LaTeX syntax inserted by ed needs to be modified for your specific table

3
  • 1
    Hm, and can this be used on an windows computer too? How?
    – Mensch
    Aug 23, 2019 at 19:22
  • 1
    Yes, there are multiple options to get a Unix-type shell on Windows. You can run it in the shell of the Windows Subsystem for Linux (WSL) that is installed through the Windows app store. An alternative is Cygwin.
    – jsb
    Aug 23, 2019 at 19:25
  • 2
    +1 for recommending using tsv files. Jun 20, 2020 at 15:09
3

Use python, specifically pandas.

import pandas as pd    
csv_table = pd.read_csv("data.csv")
print(csv_table.to_latex(index=False))

to_latex returns a string copy and paste or alternatively you could write it to a file

with open("csv_table.tex", 'w') as f:
    f.write(csv_table.to_latex(index=False))
2

To update Irwin's answer:

The service http://www.tablesgenerator.com/latex_tables lets you create latex-based tables in a GUI and then export the code. It also lets you upload a CSV and converts it automatically.

You have to select "File"->"Import CSV file" from the GUI. It handles strings and doubles just fine.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .