7
$\begingroup$

I'm looking to import the 4th column of data from a few hundred CSV files that I have stored in various folders. I have been able to import all CSV files from a given directory through use of:

allFiles = Filenames[".csv","/filepath/"]; and list1 = Import[#,"Dataset"]&/@allFiles;

However, I haven't been able to extract the 4th column of data from the lists once the files have been imported. Is there an easier way to do this?

$\endgroup$
2
  • $\begingroup$ Hi avocado_gradient and welcome! Thanks for taking the tour. It always helps us to help you when you write an excellent question. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form as you did today. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ Commented Jun 13, 2018 at 19:12
  • $\begingroup$ There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please do your part. $\endgroup$ Commented Jun 13, 2018 at 19:15

1 Answer 1

9
$\begingroup$

TL; DR

Import[#, {"CSV", "Data", All, 4}] & 

Import specific columns

Notice in the documentation for Import and for CSV that it explicitly states that you can do

Import["file.csv",{element, subelement1, subelement2], ...}] to import subelements, specifically useful for partial data import.

Mathematica graphics

Example

Example data

data = Table[ FromDigits[{i , j}] , {i, 0, 9} , {j, 1, 10} ]; TableForm[ data , TableHeadings -> Automatic ] 

Mathematica graphics

Save to file

Export[ "Test.CSV" , data ]; 

Import

Import["Test.CSV", {"CSV", "Data", All, 4}] (*{4, 14, 24, 34, 44, 54, 64, 74, 84, 94} *) 
$\endgroup$
2
  • 3
    $\begingroup$ This will also work for the "Dataset" element, which op appeared to want to use. I strongly recommend adding "FillRows" -> True as well especially for "Dataset". Import[#, {"CSV", "Dataset", All, 4}, "FillRows" -> True] & $\endgroup$ Commented Jun 13, 2018 at 20:47
  • $\begingroup$ For multiple columns: Import[f, {"CSV", "Data", All, {co1, col2, col3, ...}}] $\endgroup$ Commented Jun 2, 2021 at 22:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.