Save 3D numpy Array to CSV

Python Numpy

In this Python program, we will understand how to save 3D numpy Array to CSV. We will use numpy.savetxt() method and pandas dataframe to_csv() method with header and CSV module Methods.

Ways to save 3D numpy Array to CSV


  • NumPy savetxt() method to save 3D numpy Array to CSV
  • Python CSV module to save 3D numpy Array to CSV
  • Python Pandas to save 3D numpy array to CSV

1. Save 3D numpy Array to CSV


In this Python program example, we are writing a 3D numpy array to CSV using the NumPy module savetxt() method. To convert a list of lists to CSV

Steps to convert numpy array to CSV

  • First we have to Import the Numpy Module using import numpy as np
  • Use NumPy savetxt() method with parameters header,delimiter =”,”,fmt =’%s’,comments=”.
  • comments=” : to elminate the # by default added to first column name.
  • The savetxt() will create a CSV in current directory with name “animal.csv”

Python Program

#Python Program to save 3D numpy Array to CSV 

import numpy as np
List_rows=np.array([['Tom', 9, 8, 'WA'],['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])
np.savetxt("animal.csv",List_rows, delimiter =",", header='Name, Age, Weight, City',fmt ='%s',comments='')

Output

Name	Age	Weight	City
Tom	     9	 8	WA
Trex	6	15	CA
Kity	7	11	WA

2. CSV module to save 3D numpy Array to CSV


In the Python program code, we are learning how to save 3D numpy Array to CSV with headers.

We have imported the CSV library using code import CSV, we have two lists that we are converting to numpy array.

  • List_rows : It contains nested list to write to CSV file rows.
  • List_columns: To write first row that is columns or header.

The open(‘animal.csv’, ‘w’) method is used to open the CSV file in write mode. The writer() method is used to write a single row or writerows() for multiple rows.

So we have written the header using the writer() method and multiple rows writerows(). It will create a CSV file in the current directory.

Python Program

#CSV module to save 3D numpy Array to CSV 
import numpy as np
import csv 

list_rows= [ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']] 
List_columns = ['Name', 'Age', 'Weight', 'City']  


 
dtype = [('Name', (np.str_, 10)), ('Marks', np.int32), ('weight', np.int32),('City', (np.str_, 5))]
 
np_array = np.array(list_rows)

np_array_col = np.array(List_columns)
  

with open('animal.csv', 'w') as csvfile: 
    write = csv.writer(csvfile) 
    write.writerow(np_array_col) 
    write.writerows(np_array) 

Output

Name	Age	Weight	City
			
Tom	    9	8	  WA
			
Trex	6	15	  CA
			
Kity	7	11	   WA

3. Python Pandas to save 3D numpy array to CSV


First, we have imported the Pandas module “import pandas as pd”. Using the panda’s module, first, we are converting a list of lists to dataframe, then using Pandas dataframe.to_csv() method to write a list of lists to csv, parameter header=List_columns passing a list of columns.

  • List_rows : It contain a list of lists to be written to CSV file rows.
  • List_columns: It conatin column names or header to be writte first row.
  • np_array_rows :converted a list of lists to numpy array that will use to write rows of csv file.
  • np_array_columns :converted the list of columns to numpy array will use to write column name or header of csv file.
  • to_csv() : pandas dataframe methd to write numpy array to csv.
    • header : to pass name of columns

Python Program

#Python Pandas to save 3D numpy array to CSV

import pandas as pd
import numpy as np
List_rows  =[ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']]
List_columns = ['Name', 'Age', 'Weight', 'City']

np_array_rows = np.array(List_rows)
np_array_columns = np.array(List_columns)

print(np_array_rows)

df = pd.DataFrame(np_array_rows)
df.to_csv('animal.csv', index=False, header=np_array_columns)

Output

Name	Age	Weight	City
Tom	9	8	WA
Trex	6	15	CA
Kity	7	11	WA

Summary

In this post, we have learned how to Save 3D numpy Array to CSV using the three different ways with headers.

  • Using Numpy.savetxt() method to save 3D numpy Array to CSV
  • CSV module to save 3D numpy Array to CSV.
  • Python Pandas to save 3D numpy array to CSV.