Data Science Tutorial menu

Pandas Series and DataFrames

pandas is the main Python library for working with tables of data. Its two building blocks are the Series and the DataFrame.

Importing pandas

By tradition, pandas is imported with the short name pd:

Example

python
import pandas as pd

Series: One Column

A Series is a list of values with labels, called the index:

Example

python
prices = pd.Series([160, 340, 520], index=["Sugar", "Rice", "Tea"])
print(prices)
print(prices["Rice"])
Output
Sugar    160
Rice     340
Tea      520
dtype: int64
340

dtype: int64 means the values are whole numbers. You can do maths on the whole Series at once, with no loop:

Example

python
print(prices * 1.1)
Output
Sugar    176.0
Rice     374.0
Tea      572.0
dtype: float64

DataFrame: A Table

A DataFrame is a table with rows and columns, like an Excel sheet. Each column is a Series. The easiest way to create one is from a dictionary:

Example

python
data = {
    "name": ["Ali", "Ayesha", "Bilal", "Sana"],
    "city": ["Karachi", "Lahore", "Karachi", "Quetta"],
    "age": [21, 23, 19, 22],
}
df = pd.DataFrame(data)
print(df)
print(df.shape)
print(df.columns.tolist())
Output
     name     city  age
0     Ali  Karachi   21
1  Ayesha   Lahore   23
2   Bilal  Karachi   19
3    Sana   Quetta   22
(4, 3)
['name', 'city', 'age']

shape gives (rows, columns). By default the index is 0, 1, 2, 3.

Selecting Columns

Use square brackets with a column name. One name gives a Series; a list of names gives a DataFrame:

Example

python
print(df["age"])
print(df[["name", "age"]])
Output
0    21
1    23
2    19
3    22
Name: age, dtype: int64
     name  age
0     Ali   21
1  Ayesha   23
2   Bilal   19
3    Sana   22

Note the double brackets in df[["name", "age"]]. The outer brackets select, the inner brackets make a list of columns.

Selecting Rows: loc and iloc

MethodSelects byExample
locLabels (index names and column names)df.loc[1, "city"]
ilocPositions (numbers starting at 0)df.iloc[1, 1]

Example

python
print(df.loc[1])
print(df.loc[0, "city"])
print(df.iloc[2, 0])
Output
name    Ayesha
city    Lahore
age         23
Name: 1, dtype: object
Karachi
Bilal

You can also take a range of rows: df.loc[0:2] gives rows 0, 1 and 2 (with loc, the end is included), while df.iloc[0:2] gives rows 0 and 1 (with iloc, the end is excluded, like Python lists).

Adding Columns

Assign a list, or a calculation using other columns:

Example

python
df["passed"] = [True, True, False, True]
df["age_next_year"] = df["age"] + 1
print(df)
Output
     name     city  age  passed  age_next_year
0     Ali  Karachi   21    True             22
1  Ayesha   Lahore   23    True             24
2   Bilal  Karachi   19   False             20
3    Sana   Quetta   22    True             23

Removing Columns

drop removes columns. It returns a new DataFrame, so save the result:

Example

python
df = df.drop(columns=["age_next_year"])
print(df.columns.tolist())
Output
['name', 'city', 'age', 'passed']

Quick Summary

TaskCode
Create a DataFramepd.DataFrame(dictionary)
Sizedf.shape
Column namesdf.columns
One columndf["age"]
Several columnsdf[["name", "age"]]
Row by labeldf.loc[1]
Row by positiondf.iloc[1]
New columndf["new"] = ...

Exercise

Exercise

In a DataFrame df, which code selects the value in the first row (position 0) and the second column (position 1)?

Learn Data Science with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp