TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas NumPy

Pandas NumPy with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

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

# import the "array" for demonstrating array operations
import array
# initializing an array with array values and signed integers
arr = array.array('l', [2, 4, 6, 8, 10, 12]) 
# print the original array
print ("New created array: ",end="")
for l in range (0,5):
print (arr[l], end=" ")
print ("\r")

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:

# importing pandas as pd
import pandas as pd
# dictionary of lists 
dict = {'name':["Smith", "William", "Phill", "Parker"], 
        'age': ["28", "39", "34", "36"]} 
info = pd.DataFrame(dict, index = [True, True, False, True]) 
print(info)

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[]

# importing pandas as pd
import pandas as pd
# dictionary of lists 
dict = {'name':["Smith", "William", "Phill", "Parker"], 
        'age': ["28", "39", "34", "36"]} 
info = pd.DataFrame(dict, index = [True, True, False, True]) 
# accessing a dataframe using .loc[] function  
print(info.loc[True])

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

numpy.reshape(a, newshape, order='C')

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

import numpy as np
arr = np.arange(16)
print("The Original array is: \n", arr)
# shape array with 2 rows and 8 columns
arr = np.arange(16).reshape(2, 8)
print("\nreshapedarray: \n", arr)
# shape array with 2 rows and 8 columns
arr = np.arange(16).reshape(8 ,2)
print("\nreshaped array: \n", arr)

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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf