Pandas Tutorial - Part 1

In this tutorial, we will cover pandas

Pandas is one of the most essential libraries for data analysis in Python. We will cover many different Pandas concepts in this Pandas blog series.

Let's first cover some basic Python data structures such as tuples, lists, dictionaries, and sets.

An Overview of Basic Python Data Structures

Python provides several built-in data structures that allow you to store and organize data in different ways. Here is a quick overview of some of the most commonly used Python data structures:

Tuples

Tuples are immutable ordered sequences of elements. They are defined using parentheses () and elements are separated by commas.

my_tuple = (1, 2, 3)

Some common tuple methods:

  • count - returns number of occurrences of value
  • index - returns index of value

Lists

Lists are mutable ordered sequences of elements. They are defined using square brackets [] and elements are separated by commas.

my_list = [1, 2, 3] 

Some common list methods:

  • append - adds element to end of list
  • insert - inserts element at index
  • pop - removes and returns element at index
  • remove - removes first occurrence of value
  • sort - sorts list in place

Lists support slicing which allows you to retrieve subsets of elements from a list. Slicing uses the syntax [start:stop:step] where:

  • start is the index to start slicing (default is 0)
  • stop is the index to end slicing (default is length of list)
  • step is the increment (default is 1)
my_list = [1, 2, 3, 4, 5]

# First 3 elements 
my_list[:3] 

# Last 2 elements
my_list[-2:]

# Every other element 
my_list[::2] 

Slicing is useful for retrieving specific portions of a list without modifying the original list. Some common examples include:

  • Getting a subset of list elements
  • Skipping over elements
  • Creating copies of a list
  • Reversing a list

Slicing provides a flexible way to work with segments of lists in Python. Mastering different slicing patterns is helpful for efficiently accessing and manipulating list data.

Dictionaries

Dictionaries are unordered collections of key-value pairs. They are defined using curly braces {} where keys and values are separated by a colon :.

my_dict = {"name": "John", "age": 30}

Some common dictionary methods:

  • get - returns value for key
  • keys - returns list of keys
  • values - returns list of values
  • items - returns list of tuples of key-value pairs
  • clear - removes all entries

Sets

Sets are unordered collections of unique elements. They are defined using curly braces {}.

my_set = {1, 2, 3} 

Some common set methods:

  • add - adds element
  • remove - removes element
  • union - returns union of two sets
  • intersection - returns intersection of two sets

These core data structures provide a solid foundation for writing Python programs. Knowing when to use each structure and the methods available is key to writing efficient Python code.