# Change data type of given numpy array

## Change data type of given numpy array

In this post, we are going to see the ways in which we can change the dtype of the given numpy array. In order to change the dtype of the given array object, we will use `numpy.astype()` function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.

Problem #1 : Given a numpy array whose underlying data is of `'int32'` type. Change the dtype of the given object to `'float64'`.

Solution : We will use `numpy.astype()` function to change the data type of the underlying data of the given numpy array.

 `# importing the numpy library as np` `import` `numpy as np` ` ` `# Create a numpy array` `arr ``=` `np.array([``10``, ``20``, ``30``, ``40``, ``50``])` ` ` `# Print the array` `print``(arr)`

Output :

Now we will check the dtype of the given array object.

 `# Print the dtype` `print``(arr.dtype)`

Output :

As we can see in the output, the current dtype of the given array object is ‘int32’. Now we will change this to ‘float64’ type.

 `# change the dtype to 'float64'` `arr ``=` `arr.astype(``'float64'``)` ` ` `# Print the array after changing` `# the data type` `print``(arr)` ` ` `# Also print the data type` `print``(arr.dtype)`

Output :

Problem #2 : Given a numpy array whose underlying data is of `'int32'` type. Change the dtype of the given object to `'complex128'`.

Solution : We will use `numpy.astype()` function to change the data type of the underlying data of the given numpy array.

 `# importing the numpy library as np` `import` `numpy as np` ` ` `# Create a numpy array` `arr ``=` `np.array([``10``, ``20``, ``30``, ``40``, ``50``])` ` ` `# Print the array` `print``(arr)`

Output :

Now we will check the dtype of the given array object.

 `# Print the dtype` `print``(arr.dtype)`

Output :

As we can see in the output, the current dtype of the given array object is ‘int32’. Now we will change this to ‘complex128’ type.

 `# change the dtype to 'complex128'` `arr ``=` `arr ``=` `arr.astype(``'complex128'``)` ` ` `# Print the array after changing` `# the data type` `print``(arr)` ` ` `# Also print the data type` `print``(arr.dtype)`

Output :

Last Updated on October 26, 2021 by admin

## Load CSV data into List and Dictionary using PythonLoad CSV data into List and Dictionary using Python

Load CSV data into List and Dictionary using Python CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each

## Python – Count occurrences of a character in stringPython – Count occurrences of a character in string

Python | Count occurrences of a character in string Given a string, the task is to count the frequency of a single character in that string. This particular operation on string is quite useful in many applications such as removing

## Convert JSON to dictionary in PythonConvert JSON to dictionary in Python

Convert JSON to dictionary in Python JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through

## Python – Convert dictionary object into stringPython – Convert dictionary object into string

Python | Convert dictionary object into string Dictionary is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of

## Python – Initializing dictionary with empty listsPython – Initializing dictionary with empty lists

Python | Initializing dictionary with empty lists In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list

## Python program to count Even and Odd numbers in a ListPython program to count Even and Odd numbers in a List

Given a list of numbers, write a Python program to count Even and Odd numbers in a List. Example: Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 2 Input: list2 = [12, 14, 95,

## Basic calculator program using PythonBasic calculator program using Python

Basic calculator program using Python Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input. Approach : User choose the desired operation. Options 1, 2, 3 and 4 are

## Backward iteration in PythonBackward iteration in Python

Backward iteration in Python The iteration of numbers is done by looping techniques in Python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backward and having shorthands to do so can be