C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas NumPyNumerical 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 indexingBoolean 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:
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 arraysReshaping arrays are used to reshape the array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameters
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
|