Sick Gaming
[Tut] How to Convert a CSV to NumPy Array in Python? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Convert a CSV to NumPy Array in Python? (/thread-99684.html)



[Tut] How to Convert a CSV to NumPy Array in Python? - xSicKxBot - 07-11-2022

How to Convert a CSV to NumPy Array in Python?

<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;459615&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<p>Given a CSV file (e.g., stored in the file with name <code>'my_file.csv'</code>).</p>
<pre class="wp-block-preformatted"><code><strong>INPUT</strong>: file 'my_file.csv'</code>
<code>9,8,7
6,5,4
3,2,1</code></pre>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-1024x576.jpg" alt="How to Convert a CSV to NumPy Array in Python?" class="wp-image-459653" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-1024x576.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray-768x432.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/convert_csv_nparray.jpg 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p><strong>Challenge</strong>: How to convert it to a NumPy Array?</p>
<pre class="wp-block-preformatted"><code><strong>OUTPUT</strong>: 2D NumPy Array</code>
<code>[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]</code></pre>
<h2>Method 1: np.loadtxt()</h2>
<div class="wp-block-cover aligncenter is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<div class="wp-block-cover__inner-container">
<p class="has-text-align-center has-base-3-color has-text-color" style="font-size:60px"><strong>np.loadtxt()</strong></p>
</div>
</div>
<p class="has-global-color-8-background-color has-background">You can convert a CSV file to a NumPy array simply by calling <code><a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html" data-type="URL" data-id="https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html" target="_blank">np.loadtxt()</a></code> with two arguments: the <code>filename</code> and the <code>delimiter</code> string. For example, the expression <code>np.loadtxt('my_file.csv', delimiter=',')</code> returns a NumPy array from the <code>'my_file.csv'</code> with delimiter symbols <code>','</code>. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]</code></pre>
<h2>Method 2: np.loadtxt() with Header</h2>
<div class="wp-block-cover aligncenter is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<div class="wp-block-cover__inner-container">
<p class="has-text-align-center has-base-3-color has-text-color" style="font-size:60px"><strong>np.loadtxt() + header</strong></p>
</div>
</div>
<p class="has-global-color-8-background-color has-background">You can convert a CSV file with first-line header to a NumPy array by calling <code><a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html" data-type="URL" data-id="https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html" target="_blank">np.loadtxt()</a></code> with three arguments: the <code>filename</code>, <code>skiprows=1</code> to skip the first line (header), and the <code>delimiter</code> string. For example, the expression <code>np.loadtxt('my_file.csv', skiprows=1, delimiter=',')</code> returns a NumPy array from the <code>'my_file.csv'</code> with delimiter symbols <code>','</code> while skipping the first line.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="834" height="916" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-51.png" alt="" class="wp-image-459626" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-51.png 834w, https://blog.finxter.com/wp-content/uploads/2022/07/image-51-273x300.png 273w, https://blog.finxter.com/wp-content/uploads/2022/07/image-51-768x844.png 768w" sizes="(max-width: 834px) 100vw, 834px" /><figcaption><em><strong>Figure</strong>: Skip the first header line in the CSV using the <code>skiprows</code> argument of the <code>np.loadtxt()</code> function.</em></figcaption></figure>
</div>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np array = np.loadtxt('my_file.csv', skiprows=1, delimiter=',')
print(array)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]</code></pre>
<h2>Method 3: CSV Reader</h2>
<div class="wp-block-cover aligncenter is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<div class="wp-block-cover__inner-container">
<p class="has-text-align-center has-base-3-color has-text-color" style="font-size:60px"><strong>CSV Reader</strong></p>
</div>
</div>
<p class="has-global-color-8-background-color has-background">To convert a CSV file <code>'my_file.csv'</code> into a list of lists in Python, use the <code>csv.reader(file_obj)</code> method to create a CSV file reader. Then convert the resulting object to a list using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list/" target="_blank">list()</a></code> constructor. As a final step, you can convert the nested list to a NumPy array by using the <code>np.array(list)</code> constructor.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(reader) print(lst)</pre>
<p>The output is the <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank" rel="noreferrer noopener">list of lists</a>:</p>
<pre class="wp-block-preformatted"><code>[['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]</code></pre>
<p>Now, if you need to convert it to a NumPy array, you can simply use the <code>np.array()</code> function on the newly-created list like so:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">array = np.array(lst)
print(array)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[['9' '8' '7'] ['6' '5' '4'] ['3' '2' '1']]</code></pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial</strong>: <a href="https://blog.finxter.com/python-convert-csv-to-list-of-lists/" data-type="post" data-id="434167" target="_blank" rel="noreferrer noopener">How to Convert CSV to List of Lists in Python</a></p>
<h2>Method 4: np.genfromtxt()</h2>
<div class="wp-block-cover aligncenter is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<div class="wp-block-cover__inner-container">
<p class="has-text-align-center has-base-3-color has-text-color" style="font-size:60px"><strong>np.genfromtxt()</strong></p>
</div>
</div>
<p class="has-global-color-8-background-color has-background">You can convert a CSV file to a NumPy array simply by calling <a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html" data-type="URL" data-id="https://numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html" target="_blank"><code>np.genfromtxt()</code></a> with two arguments: the <code>filename</code> and the <code>delimiter</code> string. For example, the expression <code>np.genfromtxt('my_file.csv', delimiter=',')</code> returns a NumPy array from the <code>'my_file.csv'</code> with delimiter symbol <code>','</code>. </p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]</code></pre>
<h2>Method 5: Pandas read_csv() and df.to_numpy()</h2>
<div class="wp-block-cover aligncenter is-light"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img loading="lazy" width="1024" height="536" class="wp-block-cover__image-background wp-image-451029" alt="" src="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg" data-object-fit="cover" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-1024x536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-300x157.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit-768x402.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/07/python-logo-generic-reddit.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<div class="wp-block-cover__inner-container">
<p class="has-text-align-center has-base-3-color has-text-color" style="font-size:60px"><strong>read_csv() and df.to_numpy()</strong></p>
</div>
</div>
<p class="has-global-color-8-background-color has-background">A quick and efficient way to read a CSV to a NumPy array is to combine Pandas’ <code><a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655" target="_blank" rel="noreferrer noopener">pd.read_csv()</a></code> function to read a given CSV file to a DataFrame with the <code><a href="https://blog.finxter.com/three-ways-to-transform-pandas-dataframes-to-arrays-effortlessly/" data-type="post" data-id="229550" target="_blank" rel="noreferrer noopener">df.to_numpy()</a></code> function to convert the Pandas DataFrame to a NumPy array.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.read_csv('my_file.csv', header=None)
array = df.to_numpy() print(array)
</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[[9 8 7] [6 5 4] [3 2 1]]</code></pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial</strong>: <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655">17 Ways to Read a CSV File to a Pandas DataFrame</a></p>
<h2>Summary</h2>
<p>We have seen five ways to convert a CSV file to a 2D NumPy array:</p>
<ul>
<li><strong>Method 1</strong>: <code>np.loadtxt()</code></li>
<li><strong>Method 2</strong>: <code>np.loadtxt()</code> with Header</li>
<li><strong>Method 3</strong>: CSV Reader</li>
<li><strong>Method 4</strong>: <code>np.genfromtxt()</code></li>
<li><strong>Method 5</strong>: Pandas <code>read_csv()</code> and <code>df.to_numpy()</code></li>
</ul>
<p>Our preferred way is <code>np.loadtxt()</code> for its simplicity and Pandas for its extensibility.</p>
</div>


https://www.sickgaming.net/blog/2022/07/07/how-to-convert-a-csv-to-numpy-array-in-python/