Delete rows and columns of NumPy ndarray Last Updated : 21 Apr, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to delete the specified rows and columns in an n-dimensional array. We are going to delete the rows and columns using numpy.delete() method. Syntax: numpy.delete(array_name, obj, axis=None) Let's discuss with the help of some examples: Example 1: Program to create a 2-dimensional array (3 rows and 4 columns) with NumPy and delete the specified row. Python3 # importing numpy module import numpy as np # create an array with integers # with 3 rows and 4 columns a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(a) # delete 0 th row data = np.delete(a, 0, 0) print("data after 0 th row deleted :", data) # delete 1 st row data = np.delete(a, 1, 0) print("data after 1 st row deleted :", data) # delete 2 nd row data = np.delete(a, 2, 0) print("data after 2 nd row deleted :", data) Output: Example 2: Program to create a 2-dimensional array (6 rows and 2 columns) with NumPy and delete the specified columns. Python3 # importing numpy module import numpy as np # create an array with integers with # 6 rows and 2 columns a = np.array([[1, 2], [5, 6], [9, 10, ], [78, 90], [4, 89], [56, 43]]) print(a) # delete 0 th column data = np.delete(a, 0, 1) print("data after 0 th column deleted :", data) # delete 1 st column data = np.delete(a, 1, 1) print("data after 1 st column deleted :", data) Output: Example 3: Delete both 1 row and 1 column. Python3 # importing numpy module import numpy as np # create an array with integers # with 3 rows and 3 columns a = np.array([[67, 65, 45], [45, 67, 43], [3, 4, 5]]) print("Original\n", a) # delete 1 st row data = np.delete(a, 0, 0) print("data after 1 st row deleted :\n", data) # delete 1 st column data = np.delete(a, 0, 1) print("data after 1 st column deleted :\n", data) Output: Example 4: We can delete n number of rows at a time by passing row numbers as a list in the obj argument. Syntax: numpy.delete(array_name, [row1,row2,.row n], axis=None) Python3 # importing numpy module import numpy as np # create an array with integers # with 3 rows and 3 columns a = np.array([[67, 65, 45], [45, 67, 43], [3, 4, 5]]) print("Original\n", a) # delete 1 st row and 2 nd # row at a time data = np.delete(a, [0, 1], 0) print("data after 1 st and 2 ns row deleted :\n", data) Output: Example 5: We can delete n number of columns at a time by passing column numbers as a list in the obj argument. Syntax: numpy.delete(array_name, [column number1,column number2,.column number n], axis=None) Python3 # importing numpy module import numpy as np # create an array with integers # with 3 rows and 3 columns a = np.array([[67, 65, 45], [45, 67, 43], [3, 4, 5]]) print("Original\n", a) # delete 1 st column and 3 rd # column at a time data = np.delete(a, [0, 2], 1) print("data after 1 st and 3 rd column deleted :\n", data) Output: Comment More infoAdvertise with us Next Article Delete rows and columns of NumPy ndarray P pulamolusaimohan Follow Improve Article Tags : Python Python-numpy Python numpy-ndarray Practice Tags : python Similar Reads Python - Numpy Array Column Deletion Given a numpy array, write a programme to delete columns from numpy array. Examples - Input: [['akshat', 'nikhil'], ['manjeeet', 'akash']] Output: [['akshat']['manjeeet']] Input: [[1, 0, 0, 1, 0], [0, 1, 2, 1, 1]] Output: [[1 0 1 0][0 2 1 1]] Given below are various methods to delete columns from nu 2 min read Numpy ndarray.tobytes() function | Python numpy.ndarray.tobytes() function construct Python bytes containing the raw data bytes in the array. Syntax : numpy.ndarray.tobytes(order='C') Parameters : order : [{âCâ, âFâ, None}, optional] Order of the data for multidimensional arrays: C, Fortran, or the same as for the original array. Return : P 1 min read NumPy ndarray.T | Get View of Transposed Array The NumPy ndarray.T attribute finds the view of the transposed Array. It can transpose any array having a dimension greater than or equal to 2. It works similarly to the numpy.transpose() method but it is easy and concise to use. SyntaxSyntax: ndarray.T Returns Transpose of given arrayExamplesLet's 1 min read How to Set Axis for Rows and Columns in NumPy ? In this article, we are going to see how to set the axis for rows and columns in NumPy. Functions Usednp.array(object): to create a NumPy array, the object is the parameter that contains the arraynp.reshape(rows, columns): to reshape the array into the specified number of rows and columns. Here in t 3 min read Python | Ways to add row/columns in numpy array Given a Numpy array, the task is to add rows/columns basis on requirements to the Numpy array. Let's see a few examples of this problem in Python. Add columns in the Numpy arrayMethod 1: Using np.append()Python3 import numpy as np ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]]) # printing 5 min read How to Convert a Dataframe Column to Numpy Array NumPy and Pandas are two powerful libraries in the Python ecosystem for data manipulation and analysis. Converting a DataFrame column to a NumPy array is a common operation when you need to perform array-based operations on the data. In this section, we will explore various methods to achieve this t 2 min read Python - Iterate over Columns in NumPy Numpy (abbreviation for 'Numerical Python') is a library for performing large-scale mathematical operations in a fast and efficient manner. This article serves to educate you about methods one could use to iterate over columns in an 2D NumPy array. Since a single-dimensional array only consists of l 3 min read Numpy ndarray.flatten() function in Python The flatten() function is used to convert a multi-dimensional NumPy array into a one-dimensional array. It creates a new copy of the data so that original array stays unchanged. If your array has rows and columns or even more dimensions, then flatten() line up every single value into a straight list 3 min read Convert Numpy Array to Dataframe Converting a NumPy array into a Pandas DataFrame makes our data easier to understand and work with by adding names to rows and columns and giving us tools to clean and organize it.In this article, we will take a look at methods to convert a numpy array to a pandas dataframe. We will be discussing tw 4 min read numpy.ndarray.view() in Python numpy.ndarray.view() helps to get a new view of array with the same data. Syntax: ndarray.view(dtype=None, type=None)Parameters: dtype : Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. type : Python type, opti 3 min read Like