Pandas-Handling data in Python

pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language.

In this Blog, lets understand how to deal with data/ data sets using pandas.

Installing pandas

run the below command to install pandas

pip install pandas

Alternatively you may install Anaconda, which has pandas inbuilt.

Data Structures in Pandas

Pandas deal with 3 different data structures.

  1. Series: One Dimensional array with homogeneous data. Values of the series data structure can be altered but not the size.
  2. DataFrame: This is a 2-D array with Heterogeneous data . For example,It can hold and handle SQL table data.
  3. Panel: This is a 3-D array with heterogeneous data. Panel can be illustrated as a container of DataFrame.

Importing Pandas

import pandas as pd

The below codes uses pd to represent pandas

Read a csv file


Write to csv file


Now the data in the csv is copied to df


Prints the content of the Dataframe(ideally the whole data from the csv file)


Prints the column names along with their Data types

df = df.drop("No. of Fire and Rescue Stations", axis=1)

Drops the column by name ‘No. of Fire and Rescue Stations


returns the shape of the dataframe (rows,columns)


returns the non-null data counts

df['No. of Fire and Rescue Stations']=0

creates a new column

df.columns = ['Column 1', 'Column 2', 'Column 3']

Renames the columns of a dataframe. Specify the new names.

#Sum of values in a DataFrame
df.sum()#Lowest value of a DataFrame
df.min()#Highest value
df.max()#Index of the lowest value
df.idxmin()#Index of the highest value
df.idxmax()#Statistical summary of the DataFrame, with quartiles, median, etc.
df.describe()#Average values
df.mean()#Median values


df.apply(lambda x: x.replace('a', 'b'))

Replace the values in dataframe.

Selecting rows by position


Selecting last two rows by position


Selecting rows by index


Iterate over the dataframe

for index,row in df.iterrows():
   print("{0} recieved : {1} Fire                  calls".format(row["Division"],row["Fire Calls Total"]))

Sort Values

df.sort_values("Fire Calls Total",ascending=False)