Pandas

pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

We can analyze data in pandas with:

  1. Series

  2. DataFrames

Series:

Series is one dimensional(1-D) array defined in pandas that can be used to store any data type.

Code #1: Creating Series

# Program to create series 
import pandas as pd  # Import Panda Library 
  
# Create series with Data, and Index 
a = pd.Series(Data, index = Index)  

Here, Data can be:

  1. A Scalar value which can be integerValue, string

  2. A Python Dictionary which can be Key, Value pair

  3. A Ndarray

Code #2: When Data contains scalar values

OUTPUT:

Code #3: When Data contains Dictionary

OUTPUT:

Code #4:When Data contains Ndarray

OUTPUT:

eggs 30

apples 6

milk Yes

bread No

dtype: object

Groceries has shape: (4,)

Groceries has dimension: 1

Groceries has a total of 4 elements

The data in Groceries is: [30 6 'Yes' 'No']

The index of Groceries is: Index(['eggs', 'apples', 'milk', 'bread'], dtype='object')

Is bananas an index label in Groceries: False

Is bread an index label in Groceries: True

How many eggs do we need to buy: 30

Do we need milk and bread:

milk Yes

bread No

dtype: object

We can also delete items from a Pandas Series by using the .drop() method. The Series.drop(label) method removes the given label from the given Series. We should note that the Series.drop(label) method drops elements from the Series out of place, meaning that it doesn't change the original Series being modified. Let's see how this works:

Original Grocery List: eggs 30 apples 6 milk Yes bread No dtype: object

We remove apples (out of place): eggs 30 milk Yes bread No dtype: object

Grocery List after removing apples out of place: eggs 30 apples 6 milk Yes bread No dtype: object

We can delete items from a Pandas Series in place by setting the keyword inplace to True in the .drop() method. Let's see an example:

Original Grocery List: eggs 30 apples 6 milk Yes bread No dtype: object

Grocery List after removing apples in place: eggs 30 milk Yes bread No dtype: object

Arithmetic Operations on Pandas Series

Just like with NumPy ndarrays, we can perform element-wise arithmetic operations on Pandas Series. In this lesson we will look at arithmetic operations between Pandas Series and single numbers. Let's create a new Pandas Series that will hold a grocery list of just fruits.

apples 10 oranges 6 bananas 3 dtype: int64

We can now modify the data in fruits by performing basic arithmetic operations. Let's see some examples

Original grocery list of fruits: apples 10 oranges 6 bananas 3 dtype: int64

fruits + 2: apples 12 oranges 8 bananas 5 dtype: int64

fruits - 2: apples 8 oranges 4 bananas 1 dtype: int64

fruits * 2: apples 20 oranges 12 bananas 6 dtype: int64

fruits / 2: apples 5.0 oranges 3.0 bananas 1.5 dtype: float64

You can also apply mathematical functions from NumPy, such assqrt(x), to all elements of a Pandas Series.

Original grocery list of fruits: apples 10 oranges 6 bananas 3 dtype: int64

EXP(X) = apples 22026.465795 oranges 403.428793 bananas 20.085537 dtype: float64

SQRT(X) = apples 3.162278 oranges 2.449490 bananas 1.732051 dtype: float64

POW(X,2) = apples 100 oranges 36 bananas 9 dtype: int64

DataFrames:

DataFrames is two-dimensional(2-D) data structure defined in pandas which consists of rows and columns.

Code #1: Creation of DataFrame

Here, Data can be:

  1. One or more dictionaries

  2. One or more Series

  3. 2D-numpy Ndarray

Code #2: When Data is Dictionaries

OUTPUT:

Code #3: When Data is Series

OUTPUT:

Code #4: When Data is 2D-numpy ndarray:

Note: One constraint has to be maintained while creating DataFrame of 2D arrays – Dimensions of 2D array must be same

OUTPUT:

Last updated

Was this helpful?