Pandas in Python

Pruthvi Vikram
2 min readFeb 4, 2021

10 minutes of learning, by Pruthvi Vikram (pruthvivikram.com)

Image source: onlineITguru.com

What is Pandas?

Pandas is a Python library used especially for data manipulation and data analysis. Pandas is built on top of the Numpy library.

Why use Pandas?

Pandas is faster than other data structures like lists. Pandas handle missing values in a different way. We will soon see how it's done.

Important data structures of Pandas

There are two main data structures that you should remember and understand to work with Pandas.
1. Series
2. Data Frame

Series and Data Frames

Both are data structures of Pandas that are used to store, manipulate and perform analysis of data. I would say that the data frame is more complicated than the Series. But, you cannot understand data frames without understanding Series.

I prefer learning by doing over learning by reading. So, let’s jump into coding right away.

Step 1: Import pandas library

# Import Pandas
import pandas as pd

Step 2: Create a Series object

# Create Series
animals = pd.Series(["Zebra", "Elephant", "Cheeta"])
type(animals_series)
o/p :
pandas.core.series.Series

As you can see, the series object is created using pd.Series() method. Hmmm… Now the question is, whether a Series object can be created out of existing lists or dictionaries. Well, the answer is yes.

# Series from Lists
numbers = [1, 2, 3, 4, 5]
ser_num = pd.Series(numbers)
print(“Type of numbers: “ , type(numbers), “\nType of ser_num: “ , type(ser_num))
o/p :
Type of numbers: <class 'list'>
Type of ser_num: <class 'pandas.core.series.Series'>
# Series from Dictionaries
sports = {
'india' : 'Cricket',
'europe': 'Soccer',
'USA' : 'Football'
}
ser_sports = pd.Series(sports)
print("Type of sports: " , type(sports), "\nType of ser_sports: " , type(ser_sports))
o/p:
Type of sports: <class 'dict'>
Type of ser_sports: <class 'pandas.core.series.Series'>

Step 3: Querying a Series

Remember .loc and .iloc methods. We use them to query from any Series.

.iloc — used to retrieve using a position

.loc — used to retrieve using a key

1. Retrieve using .iloc

ser_sports.iloc[1]o/p : 
'Soccer'

2. Retrieve using .loc

ser_sports.loc[‘india’]o/p:
'Cricket'

--

--