Posts

Slicing in Python

Image
 You can slice pretty much any sequence in Python. A sequence is something that you can index from 0 to len(sequence)-1. Lists, tuples, and strings are all examples of sequences. Syntax: sequence[start:stop:step]   Let say we have a variable fruits that points to a list:   If we put a colon and another number inside the square brackets, we're slicing this list instead of indexing it: Indexes start from 0. for example, from "Hello, World!" we need to extract "world" for that use sequence[7,12] as shown below. In the second example, we have given step size as 2, so it will check for 2 step size. The output displayed as [1,3]

Sets in Python

Image
A set is an unordered collection of elements without any duplicates. Sets are especially useful for performing logical operations like finding the union, intersection, or difference between collections of elements. For example, sets could be used to determine mutual friends on a social networking site. Syntax : x = set(iterables) or x = {value1,value2,value3......valuen} There are several ways to create a set, which include: Using the built-in  set()  function and passing in an optional iterable parameter. Hard-coding a set with dictionary-like syntax ({}) where each element is unique.   Here, it is not printed the GCP name twice, It is a duplicate record & it will only display the Unique records. The other methods in the sets defined below .difference(): Returns a new set of objects unique to a given set when compared to others. .intersection(): Returns a new set with objects that exist inside two or more sets .union(): Returns a new...

Dictionaries in Python

Image
A dictionary is a data set of key-value pairs. It provides a way to map pieces of data to each other and allows for quick access to values associated with keys. In Python, dictionaries are dynamic and mutable, which means they can change. Note:  As of Python version 3.7, dictionaries are ordered based on insertion, but this is not the case in previous versions.   Syntax: dictionary_name = { key1: value1,   key2: value2,   key3: value3 } Each entry in a dictionary is a key-value pair. Each pair is separated by a comma. Dictionary keys must be immutable types such as numbers and strings because keys should not change. Keys cannot be lists because lists are mutable, and it will raise a TypeError. Values can be any type, such as strings, numbers, lists, and even other dictionaries. Dictionary Creation: 1. dic = {} 2.x = dict() A same dictionary can be created as below: t1 = {1:'python',2:'Java',3:'Hadoop','4:'Aws'} Here {1,2,3} are keys and {...

Datatypes in Python

Image
Python is a strongly typed language, in the sense that at runtime it prevents typing errors and it engages in little implicit type conversion or casting, i.e. converting one type to another without a specific call to a conversion function. Python includes the following categories of built-in data types: String type: str Boolean type: bool Binary types: bytes, bytearray, memoryview Number types: int, float, complex Sequence Types: list, range, tuple Set types: set, frozenset Dictionary type: dict   type() The type() function can be used to retrieve the data type of an object: isinstance() The isinstance() function can be used to test if an object is an instance of a specified type. This will print a boolean value for each function call, indicating if the object is an instance of the given type:

Python -- Lists and Tuples

Image
 List and Tuple are built-in container types defined in Python. Objects of both these types can store different other objects that are accessible by index. List as well as tuple is a sequence data type, just as string. List as well as tuple can store objects which need not be of same type. List  : A List is an ordered collection of items (which may be of same or different types) separated by comma and enclosed in square brackets. Tuple:  Tuple looks similar to list. The only difference is that comma separated items of same or different type are enclosed in parentheses. Differences between List & Tuple: The obvious difference is the use of square brackets [] in List and parentheses () in tuple as enclosures. However, the important difference is that List as a mutable object and Tuple is an immutable object. If contents of an object can be modified in place, after it has been instantiated, is a mutable object. On the other hand, any operation on immutable object...

EDA using Pandas

Image
  1. Read data # Read data from csv file df = pd.read_csv('IMDB-Movie-Data.csv')     # Read data with specified index. df = pd.read_csv('IMDB-Movie-Data.csv', index_col="Title")   2. View data Let’s do a quick preview of the data by using head( ) and tail( ) methods head( )  Returns the top 5 rows in the dataset by default  It can also take the number of rows to be viewed as a parameter tail( ) Returns the bottom 5 rows in the dataset by default  3.Describe() which displays all statistical summary of all numerical attributes in the dataframe. df.describe() Extract data using rows loc  and iloc are two functions that can be used to slice data from specific row indexes. loc  – locates the rows by name loc  performs slicing based explicit index. It takes string indexes to retrieve data from specified rows iloc – locates the rows by integer index iloc performs slicing based on Python’s default...

Pandas in Python

Image
Pandas is built on top of the NumPy package, meaning a lot of the structure of NumPy is used or replicated in Pandas. Data in pandas is often used to feed statistical analysis in SciPy, plotting functions from Matplotlib, and machine learning algorithms in Scikit-learn. Jupyter Notebooks offer a good environment for using pandas to do data exploration and modeling, but pandas can also be used in text editors just as easily.   Install and import We can use 2 methods to install pandas   as below   Conda install pandas   Or   !pip3 install pandas   The “!” at the beginning runs cells as if they were in a terminal.   Alternatively, if you're currently viewing this article in a Jupyter notebook you can run this cell:   To import pandas we usually import it with a shorter name since it's used so much:   Import pandas as pd pd is the alias name for the pandas, so that it'll be easy to use that whenever it's required. Th...