Summary

This document covers the Python libraries NumPy and Pandas. It explains how to create, manipulate, and access arrays and dataframes in Python, including examples of different array types and data structures.

Full Transcript

Python Pandas & Numpy NumPy NumPy is short for "Numerical Python". NumPy is a Python library used for working with arrays. Installation of NumPy C:\Users\Your Name>pip install --upgrade pip C:\Users\Your Name>pip install numpy Import NumPy import numpy arr = numpy.array([1, 2, 3,...

Python Pandas & Numpy NumPy NumPy is short for "Numerical Python". NumPy is a Python library used for working with arrays. Installation of NumPy C:\Users\Your Name>pip install --upgrade pip C:\Users\Your Name>pip install numpy Import NumPy import numpy arr = numpy.array([1, 2, 3, 4, 5]) print(arr) Output: [1 2 3 4 5] NumPy as np NumPy is usually imported under the np alias. import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) Output: [1 2 3 4 5] NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function. Use a tuple to create a NumPy array: import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr) Output: [1 2 3 4 5] 1-D Arrays Create a 1-D array containing the values 1,2,3,4,5: import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) Output: [1 2 3 4 5] Access Array Elements Get the first element from the following array: import numpy as np arr = np.array([1, 2, 3, 4]) print(arr) Get the second element from the following array. import numpy as np arr = np.array([1, 2, 3, 4]) print(arr) 2-D Arrays Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) Output: [[1 2 3] [4 5 6]] Access Array Elements import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('2nd element on 1st row: ', arr[0, 1]) Output: 2nd element on 1st dim: 2 3-D arrays Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr) Output: [[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]]] Access Array Elements Access the third element of the second array of the first array: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) print(arr[0, 1, 2]) Output: 6 NumPy Array Slicing Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end]. We can also define the step, like this: [start:end:step]. If we don't pass start its considered 0 If we don't pass end its considered length of array in that dimension If we don't pass step its considered 1 NumPy Array Slicing Slice elements from index 1 to index 5 from the following array: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5]) [2 3 4 5] Slice elements from index 4 to the end of the array: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) [5 6 7] Slice elements from the beginning to index 4 (not included): import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4]) [1 2 3 4] import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1]) [5, 6] Return every other element from index 1 to index 5: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5:2]) [2 4] From the second element, slice elements from index 1 to index 4 (not included): import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[1, 1:4]) [7 8 9] From both elements, slice index 1 to index 4 (not included), this will return a 2-D array: import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 1:4]) [[2 3 4] [7 8 9]] What is Pandas? Pandas is a Python library used for working with data sets. It has functions for analyzing, cleaning, exploring, and manipulating data. Reg.no Std Name Class 101 Ram 3 102 Kumar 2 103 Sam 3 Installation of Pandas Install it using this command: C:\Users\Your Name>pip install --upgrade pip C:\Users\Your Name>pip install pandas DataFrames Data sets in Pandas are usually multi-dimensional tables, called DataFrames. Series is like a column, a DataFrame is the whole table. Example : Create a DataFrame from two Series: import pandas as pd data = { "calories": [420, 380, 390], "duration": [50, 40, 45] } myvar = pd.DataFrame(data) print(myvar) What is a Series? A Pandas Series is like a column in a table. It is a one-dimensional array holding data of any type. Create a simple Pandas Series from a list: import pandas as pd a = [1, 7, 2] myvar = pd.Series(a) print(myvar) 0 1 1 7 2 2 dtype: int64 Import Pandas Pandas is usually imported under the pd alias. import pandas as pd mydataset = { 'cars': ["BMW", "Volvo", "Ford"], 'passings': [3, 7, 2] } myvar = pd.DataFrame(mydataset) print(myvar) cars passings 0 BMW 3 1 Volvo 7 2 Ford 2 Labels If nothing else is specified, the values are labeled with their index number. First value has index 0, second value has index 1 etc. This label can be used to access a specified value. print(myvar) With the index argument, you can name your own labels. Create your own labels: import pandas as pd a = [1, 7, 2] myvar = pd.Series(a, index = ["x", "y", "z"]) print(myvar) Labels You can also use a key/value object, like a dictionary, when creating a Series. Example : Create a simple Pandas Series from a dictionary: import pandas as pd calories = {"day1": 420, "day2": 380, "day3": 390} myvar = pd.Series(calories) print(myvar) Labels To select only some of the items in the dictionary, use the index argument and specify only the items you want to include in the Series. Create a Series using only data from "day1" and "day2": import pandas as pd calories = {"day1": 420, "day2": 380, "day3": 390} myvar = pd.Series(calories, index = ["day1", "day2"]) print(myvar) Access Series element import pandas as pd a = [1, 7, 2] myvar = pd.Series(a) print(myvar) Output: 1 Write a NumPy program to convert a list of numeric values into a one-dimensional NumPy array. Expected Output: Original List: [12.23, 13.32, 100, 36.32] One-dimensional NumPy array: [ 12.23 13.32 100. 36.32] Write a NumPy program to create an array with values ranging from 12 to 38. Expected Output: [12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37] Write a NumPy program to create an 8x8 matrix and fill it with a checkerboard pattern. Checkerboard pattern: [[0 1 0 1 0 1 0 1].......... [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

Use Quizgecko on...
Browser
Browser