How to import only the first column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a file with data given in this manner:



1.30859, 0
1.81061, 0
9.37376, 0
0.341339, 0
0.39494, 0
1.16172, 0
0.139555, 0
0.583382, 0
15.8124, 0


How do I import only the first column?



A naive combination of Import[file,"Table"], Fatten and Transpose gives an error.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a file with data given in this manner:



    1.30859, 0
    1.81061, 0
    9.37376, 0
    0.341339, 0
    0.39494, 0
    1.16172, 0
    0.139555, 0
    0.583382, 0
    15.8124, 0


    How do I import only the first column?



    A naive combination of Import[file,"Table"], Fatten and Transpose gives an error.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a file with data given in this manner:



      1.30859, 0
      1.81061, 0
      9.37376, 0
      0.341339, 0
      0.39494, 0
      1.16172, 0
      0.139555, 0
      0.583382, 0
      15.8124, 0


      How do I import only the first column?



      A naive combination of Import[file,"Table"], Fatten and Transpose gives an error.










      share|improve this question















      I have a file with data given in this manner:



      1.30859, 0
      1.81061, 0
      9.37376, 0
      0.341339, 0
      0.39494, 0
      1.16172, 0
      0.139555, 0
      0.583382, 0
      15.8124, 0


      How do I import only the first column?



      A naive combination of Import[file,"Table"], Fatten and Transpose gives an error.







      import






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 hours ago

























      asked 4 hours ago









      mattiav27

      2,12911430




      2,12911430




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The short answer is that the following will work to import your file format as an array of numbers, which I think is your intention:



          Import[filename, "Table", "Data", All, 1,"FieldSeparators" -> "", ",", ""]


          this uses the Import features to determine the field separaters to handle the braces and comma in your file content and the data elements to extract just the first column.



          An alternative which in this case looks a bit simpler is this:



          ToExpression[Import[filename,"List"]][[All,1]]


          Note that this will work just for this specific input format (where every line for itself is a valid Mathematica InputForm expression). Why it works in this case needs some explanation:



          Choosing Import Format



          As this is a text like format you have to give Mathematica some hints about how to interpret what it is reading. You can do that by choosing a suitable import format and additional options for the specific import format as in the first example. If the file extension is ".txt" the default import format will be "Text" and the whole content is interpreted as a single multiline string. Unfortunately with default settings Mathematica will not show "string characters" in output so it is difficult to see what the returned data really is. I usually check that using InputForm or FullForm on the data which will more clearly show what actually is imported:



          Import[filename,"Text"]//InputForm


          You could then work with that string and transform it to a valid Mathematica expression and convert that to an array of numbers using ToExpression, e.g.:



          ToExpression[StringJoin["", 
          StringReplace[Import[filename, "Text"], "n" -> ","], ""]]


          Using "Table" as format as you did will by default import every line as a row and separate columns by whitespace, which will in your case produce an array of strings where the entries are not valid Mathematica InputForm expressions. Thus you again need extra string manipulation before you can transform them into numbers (see the other answers for examples). Again you can use InputForm to check what Import really returns in that case. By using the options documented for the "Table" import format you can make Import with format "Table" work as intended as shown in the first example.



          Using "List" as format will import every line as a string which is handy in this case as every line is a valid Mathematica InputForm string which can be transformed directly with ToExpression without extra string manipulation.



          Working with lists in Mathematica



          Once it is clear that Import[filename,"List"] returns a list of strings it might come as a surprise that one can directly feed that to ToExpression, but looking at its documentation will show that it not only works for a single string but also for a list of strings because it has the attribute Listable (which you can check with Attributes[ToExpression]). This is why we can use ToExpression directly on the output of Import, otherwise we would have to use something like:



           ToExpression /@ Import[filename,"List"]


          Extracting first column only



          istead of transposing you can directly use Part to extract the first column. Using the All argument is not only elegant but also a very efficient way to extract columns (this of course also works at deeper levels). You of course also could use the newer Span syntax (data[[;;,1]]), but I find the All version easier to read when it applies...



          Notes about performance



          if your file is huge and performance matters there are two possible improvements:



          Using ReadList[filename,"String"] might be considerably faster as it avoids some of the Import overhead (haven't checked how much difference that make in newer versions for import format "List").



          If the file is so large that memory usage becomes an issue, using ReadLists third argument you could import in batches of rows and avoid to have to read the whole file into memory in one go. That will then be more memory efficient but slower than reading everything at once. This will only be worth the effort for really huge files, though. You can find other questions on this site with examples about how to do that.






          share|improve this answer





























            up vote
            0
            down vote













            I don't think this is the most elegant solution possible, but there you go:



            StringDrop[Import[file, "Data", All, 1], 1] // ToExpression





            share|improve this answer




















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185216%2fhow-to-import-only-the-first-column%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              The short answer is that the following will work to import your file format as an array of numbers, which I think is your intention:



              Import[filename, "Table", "Data", All, 1,"FieldSeparators" -> "", ",", ""]


              this uses the Import features to determine the field separaters to handle the braces and comma in your file content and the data elements to extract just the first column.



              An alternative which in this case looks a bit simpler is this:



              ToExpression[Import[filename,"List"]][[All,1]]


              Note that this will work just for this specific input format (where every line for itself is a valid Mathematica InputForm expression). Why it works in this case needs some explanation:



              Choosing Import Format



              As this is a text like format you have to give Mathematica some hints about how to interpret what it is reading. You can do that by choosing a suitable import format and additional options for the specific import format as in the first example. If the file extension is ".txt" the default import format will be "Text" and the whole content is interpreted as a single multiline string. Unfortunately with default settings Mathematica will not show "string characters" in output so it is difficult to see what the returned data really is. I usually check that using InputForm or FullForm on the data which will more clearly show what actually is imported:



              Import[filename,"Text"]//InputForm


              You could then work with that string and transform it to a valid Mathematica expression and convert that to an array of numbers using ToExpression, e.g.:



              ToExpression[StringJoin["", 
              StringReplace[Import[filename, "Text"], "n" -> ","], ""]]


              Using "Table" as format as you did will by default import every line as a row and separate columns by whitespace, which will in your case produce an array of strings where the entries are not valid Mathematica InputForm expressions. Thus you again need extra string manipulation before you can transform them into numbers (see the other answers for examples). Again you can use InputForm to check what Import really returns in that case. By using the options documented for the "Table" import format you can make Import with format "Table" work as intended as shown in the first example.



              Using "List" as format will import every line as a string which is handy in this case as every line is a valid Mathematica InputForm string which can be transformed directly with ToExpression without extra string manipulation.



              Working with lists in Mathematica



              Once it is clear that Import[filename,"List"] returns a list of strings it might come as a surprise that one can directly feed that to ToExpression, but looking at its documentation will show that it not only works for a single string but also for a list of strings because it has the attribute Listable (which you can check with Attributes[ToExpression]). This is why we can use ToExpression directly on the output of Import, otherwise we would have to use something like:



               ToExpression /@ Import[filename,"List"]


              Extracting first column only



              istead of transposing you can directly use Part to extract the first column. Using the All argument is not only elegant but also a very efficient way to extract columns (this of course also works at deeper levels). You of course also could use the newer Span syntax (data[[;;,1]]), but I find the All version easier to read when it applies...



              Notes about performance



              if your file is huge and performance matters there are two possible improvements:



              Using ReadList[filename,"String"] might be considerably faster as it avoids some of the Import overhead (haven't checked how much difference that make in newer versions for import format "List").



              If the file is so large that memory usage becomes an issue, using ReadLists third argument you could import in batches of rows and avoid to have to read the whole file into memory in one go. That will then be more memory efficient but slower than reading everything at once. This will only be worth the effort for really huge files, though. You can find other questions on this site with examples about how to do that.






              share|improve this answer


























                up vote
                3
                down vote



                accepted










                The short answer is that the following will work to import your file format as an array of numbers, which I think is your intention:



                Import[filename, "Table", "Data", All, 1,"FieldSeparators" -> "", ",", ""]


                this uses the Import features to determine the field separaters to handle the braces and comma in your file content and the data elements to extract just the first column.



                An alternative which in this case looks a bit simpler is this:



                ToExpression[Import[filename,"List"]][[All,1]]


                Note that this will work just for this specific input format (where every line for itself is a valid Mathematica InputForm expression). Why it works in this case needs some explanation:



                Choosing Import Format



                As this is a text like format you have to give Mathematica some hints about how to interpret what it is reading. You can do that by choosing a suitable import format and additional options for the specific import format as in the first example. If the file extension is ".txt" the default import format will be "Text" and the whole content is interpreted as a single multiline string. Unfortunately with default settings Mathematica will not show "string characters" in output so it is difficult to see what the returned data really is. I usually check that using InputForm or FullForm on the data which will more clearly show what actually is imported:



                Import[filename,"Text"]//InputForm


                You could then work with that string and transform it to a valid Mathematica expression and convert that to an array of numbers using ToExpression, e.g.:



                ToExpression[StringJoin["", 
                StringReplace[Import[filename, "Text"], "n" -> ","], ""]]


                Using "Table" as format as you did will by default import every line as a row and separate columns by whitespace, which will in your case produce an array of strings where the entries are not valid Mathematica InputForm expressions. Thus you again need extra string manipulation before you can transform them into numbers (see the other answers for examples). Again you can use InputForm to check what Import really returns in that case. By using the options documented for the "Table" import format you can make Import with format "Table" work as intended as shown in the first example.



                Using "List" as format will import every line as a string which is handy in this case as every line is a valid Mathematica InputForm string which can be transformed directly with ToExpression without extra string manipulation.



                Working with lists in Mathematica



                Once it is clear that Import[filename,"List"] returns a list of strings it might come as a surprise that one can directly feed that to ToExpression, but looking at its documentation will show that it not only works for a single string but also for a list of strings because it has the attribute Listable (which you can check with Attributes[ToExpression]). This is why we can use ToExpression directly on the output of Import, otherwise we would have to use something like:



                 ToExpression /@ Import[filename,"List"]


                Extracting first column only



                istead of transposing you can directly use Part to extract the first column. Using the All argument is not only elegant but also a very efficient way to extract columns (this of course also works at deeper levels). You of course also could use the newer Span syntax (data[[;;,1]]), but I find the All version easier to read when it applies...



                Notes about performance



                if your file is huge and performance matters there are two possible improvements:



                Using ReadList[filename,"String"] might be considerably faster as it avoids some of the Import overhead (haven't checked how much difference that make in newer versions for import format "List").



                If the file is so large that memory usage becomes an issue, using ReadLists third argument you could import in batches of rows and avoid to have to read the whole file into memory in one go. That will then be more memory efficient but slower than reading everything at once. This will only be worth the effort for really huge files, though. You can find other questions on this site with examples about how to do that.






                share|improve this answer
























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  The short answer is that the following will work to import your file format as an array of numbers, which I think is your intention:



                  Import[filename, "Table", "Data", All, 1,"FieldSeparators" -> "", ",", ""]


                  this uses the Import features to determine the field separaters to handle the braces and comma in your file content and the data elements to extract just the first column.



                  An alternative which in this case looks a bit simpler is this:



                  ToExpression[Import[filename,"List"]][[All,1]]


                  Note that this will work just for this specific input format (where every line for itself is a valid Mathematica InputForm expression). Why it works in this case needs some explanation:



                  Choosing Import Format



                  As this is a text like format you have to give Mathematica some hints about how to interpret what it is reading. You can do that by choosing a suitable import format and additional options for the specific import format as in the first example. If the file extension is ".txt" the default import format will be "Text" and the whole content is interpreted as a single multiline string. Unfortunately with default settings Mathematica will not show "string characters" in output so it is difficult to see what the returned data really is. I usually check that using InputForm or FullForm on the data which will more clearly show what actually is imported:



                  Import[filename,"Text"]//InputForm


                  You could then work with that string and transform it to a valid Mathematica expression and convert that to an array of numbers using ToExpression, e.g.:



                  ToExpression[StringJoin["", 
                  StringReplace[Import[filename, "Text"], "n" -> ","], ""]]


                  Using "Table" as format as you did will by default import every line as a row and separate columns by whitespace, which will in your case produce an array of strings where the entries are not valid Mathematica InputForm expressions. Thus you again need extra string manipulation before you can transform them into numbers (see the other answers for examples). Again you can use InputForm to check what Import really returns in that case. By using the options documented for the "Table" import format you can make Import with format "Table" work as intended as shown in the first example.



                  Using "List" as format will import every line as a string which is handy in this case as every line is a valid Mathematica InputForm string which can be transformed directly with ToExpression without extra string manipulation.



                  Working with lists in Mathematica



                  Once it is clear that Import[filename,"List"] returns a list of strings it might come as a surprise that one can directly feed that to ToExpression, but looking at its documentation will show that it not only works for a single string but also for a list of strings because it has the attribute Listable (which you can check with Attributes[ToExpression]). This is why we can use ToExpression directly on the output of Import, otherwise we would have to use something like:



                   ToExpression /@ Import[filename,"List"]


                  Extracting first column only



                  istead of transposing you can directly use Part to extract the first column. Using the All argument is not only elegant but also a very efficient way to extract columns (this of course also works at deeper levels). You of course also could use the newer Span syntax (data[[;;,1]]), but I find the All version easier to read when it applies...



                  Notes about performance



                  if your file is huge and performance matters there are two possible improvements:



                  Using ReadList[filename,"String"] might be considerably faster as it avoids some of the Import overhead (haven't checked how much difference that make in newer versions for import format "List").



                  If the file is so large that memory usage becomes an issue, using ReadLists third argument you could import in batches of rows and avoid to have to read the whole file into memory in one go. That will then be more memory efficient but slower than reading everything at once. This will only be worth the effort for really huge files, though. You can find other questions on this site with examples about how to do that.






                  share|improve this answer














                  The short answer is that the following will work to import your file format as an array of numbers, which I think is your intention:



                  Import[filename, "Table", "Data", All, 1,"FieldSeparators" -> "", ",", ""]


                  this uses the Import features to determine the field separaters to handle the braces and comma in your file content and the data elements to extract just the first column.



                  An alternative which in this case looks a bit simpler is this:



                  ToExpression[Import[filename,"List"]][[All,1]]


                  Note that this will work just for this specific input format (where every line for itself is a valid Mathematica InputForm expression). Why it works in this case needs some explanation:



                  Choosing Import Format



                  As this is a text like format you have to give Mathematica some hints about how to interpret what it is reading. You can do that by choosing a suitable import format and additional options for the specific import format as in the first example. If the file extension is ".txt" the default import format will be "Text" and the whole content is interpreted as a single multiline string. Unfortunately with default settings Mathematica will not show "string characters" in output so it is difficult to see what the returned data really is. I usually check that using InputForm or FullForm on the data which will more clearly show what actually is imported:



                  Import[filename,"Text"]//InputForm


                  You could then work with that string and transform it to a valid Mathematica expression and convert that to an array of numbers using ToExpression, e.g.:



                  ToExpression[StringJoin["", 
                  StringReplace[Import[filename, "Text"], "n" -> ","], ""]]


                  Using "Table" as format as you did will by default import every line as a row and separate columns by whitespace, which will in your case produce an array of strings where the entries are not valid Mathematica InputForm expressions. Thus you again need extra string manipulation before you can transform them into numbers (see the other answers for examples). Again you can use InputForm to check what Import really returns in that case. By using the options documented for the "Table" import format you can make Import with format "Table" work as intended as shown in the first example.



                  Using "List" as format will import every line as a string which is handy in this case as every line is a valid Mathematica InputForm string which can be transformed directly with ToExpression without extra string manipulation.



                  Working with lists in Mathematica



                  Once it is clear that Import[filename,"List"] returns a list of strings it might come as a surprise that one can directly feed that to ToExpression, but looking at its documentation will show that it not only works for a single string but also for a list of strings because it has the attribute Listable (which you can check with Attributes[ToExpression]). This is why we can use ToExpression directly on the output of Import, otherwise we would have to use something like:



                   ToExpression /@ Import[filename,"List"]


                  Extracting first column only



                  istead of transposing you can directly use Part to extract the first column. Using the All argument is not only elegant but also a very efficient way to extract columns (this of course also works at deeper levels). You of course also could use the newer Span syntax (data[[;;,1]]), but I find the All version easier to read when it applies...



                  Notes about performance



                  if your file is huge and performance matters there are two possible improvements:



                  Using ReadList[filename,"String"] might be considerably faster as it avoids some of the Import overhead (haven't checked how much difference that make in newer versions for import format "List").



                  If the file is so large that memory usage becomes an issue, using ReadLists third argument you could import in batches of rows and avoid to have to read the whole file into memory in one go. That will then be more memory efficient but slower than reading everything at once. This will only be worth the effort for really huge files, though. You can find other questions on this site with examples about how to do that.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 2 hours ago









                  Albert Retey

                  20.1k4390




                  20.1k4390




















                      up vote
                      0
                      down vote













                      I don't think this is the most elegant solution possible, but there you go:



                      StringDrop[Import[file, "Data", All, 1], 1] // ToExpression





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        I don't think this is the most elegant solution possible, but there you go:



                        StringDrop[Import[file, "Data", All, 1], 1] // ToExpression





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I don't think this is the most elegant solution possible, but there you go:



                          StringDrop[Import[file, "Data", All, 1], 1] // ToExpression





                          share|improve this answer












                          I don't think this is the most elegant solution possible, but there you go:



                          StringDrop[Import[file, "Data", All, 1], 1] // ToExpression






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 3 hours ago









                          Tofi

                          14410




                          14410



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185216%2fhow-to-import-only-the-first-column%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke