4
$\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$
  • $\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$ – rhermans Jun 13 '18 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$ – rhermans Jun 13 '18 at 19:15
7
$\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$
  • 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$ – GenericAccountName Jun 13 '18 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.