Javatpoint Logo
Javatpoint Logo

Pandas NumPy

Numerical Python (Numpy) is defined as a Python package used for performing the various numerical computations and processing of the multidimensional and single-dimensional array elements. The calculations using Numpy arrays are faster than the normal Python array.

This package is created by the Travis Oliphant in 2005 by adding the functionalities of the ancestor module Numeric into another module Numarray. It is also capable of handling a vast amount of data and convenient with Matrix multiplication and data reshaping.

NumPy is mostly written in C language, and it is an extension module of Python.

Pandas are built over numpy array; therefore, numpy helps us to use pandas more effectively.

Creating Arrays

The main task of arrays is to store multiple values in a single variable. It defines the multidimensional arrays that can be easily handled in numpy as shown in the below examples:

Example

Output:

New created array: 2 4 6 8 10

Boolean indexing

Boolean indexing is defined as a vital tool of numpy, which is frequently used in pandas. Its main task is to use the actual values of the data in the DataFrame. We can filter the data in the boolean indexing in different ways that are as follows:

  • Access the DataFrame with a boolean index.
  • Apply the boolean mask to the DataFrame.
  • Masking the data based on column value.
  • Masking the data based on the index value.

Example1

This example shows how to access the DataFrame with a boolean index:

Output:

name    age
True     Smith    28
True   William   39
False    Phill   34
True    Parker    36

Example2

This example shows how to access the DataFrame with a boolean index by using .loc[]

Output:

name    age
True     Smith    28
True   William    39
True    Parker    36

Reshaping arrays

Reshaping arrays are used to reshape the array without changing its data.

Syntax

Parameters

  • a: Defines an array that is to be reshaped.
  • newshape: Defines the new shape that should be compatible with the original shape. For the integer value, the result will be a 1-D array of that length. The one shape dimension can be -1.
  • order: It is an optional parameter that read the elements by using the index order, and place the elements into the reshaped array with the help of index order.

Returns:

It returns the reshaped array.

Example

Output:

The Original array is: 
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]
reshaped array: 
 [[ 0  1  2  3  4  5  6  7]
[ 8  9 10 11 12 13 14 15]]

reshaped array: 
 [[ 0  1]
[ 2  3]
[ 4  5]
[ 6  7]
[ 8  9]
 [10 11]
 [12 13]
 [14 15]]

Next TopicBoolean indexing




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA