71

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}
Mack
  • 1,981
  • 5
  • 19
  • 18

6 Answers6

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

someonr
  • 8,531
  • I am not quite sure I see how this would exclude specific columns from my csv file. – Mack Nov 24 '13 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 '13 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 '13 at 15:46
  • @Mack The manual says that head to column names can only be used if the header entries do not contain spaces and special characters. So we need to use a different way. I'll see what I can do. – someonr Nov 24 '13 at 15:59
  • The csvcolii commands specify which column from the Basket_ball.csv goes into the column in which position in the table (position of csvcol.. in the row/line of csvcol..). They can be equal to the number of columns specified in \begin{tabular}{l|l}% and with \bfseries Time (s) & \bfseries Zeroed time (s) (but not more). The csvcolii of the table of the does not proceed to csvcoliii but to csvcoliv and csvcolv and csvcolvi etc. – a.t. Sep 12 '19 at 11:29
  • 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! – Immanuel Weihnachten Jun 20 '20 at 15:40
  • What if I don't want to specify the columns because I just want the whole table? And how do I make it use the first row as the column headers? – Nermin Nov 16 '22 at 12:39
  • 1
    @Vrangone \bfseries is fine to use – samcarter_is_at_topanswers.xyz Nov 26 '23 at 12:55
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

percusse
  • 157,807
  • 3
    I have to admit, that this solution is cleaner. – someonr Nov 24 '13 at 16:16
  • @someonr I like your honesty, I agree. Thank you guys +1! – hhh Nov 10 '14 at 01: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 01 '16 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

Irwin
  • 231
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

jsb
  • 454
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))
Chris
  • 131
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.

philmaweb
  • 121