Pandas

Pandas DataFrame

01 · What is Pandas? File · Pandas DataFrame · 1 of 12

What is Pandas?

Pandas is a Python library mainly used for:

  1. Reading Data
  2. Cleaning data
  3. Manipulating Data
  4. Filtering data
  5. Aggregating data
  6. Combining multiple datasets
  7. Performing analysis
  8. Preparing data for machine learning
Python
import pandas as pd
We import pandas with the alias pd. This is the standard convention used by almost everyone.
02 · What is a DataFrame? File · Pandas DataFrame · 2 of 12

What is a DataFrame?

A DataFrame is a two-dimensional data structure in Pandas.

It has:

  • Rows
  • Columns

A DataFrame is basically a programmable Excel sheet.

03 · DataFrame vs Series File · Pandas DataFrame · 3 of 12

DataFrame vs Series

A Series is one-dimensional (single column).
A DataFrame is two-dimensional (rows + columns).

Python
import pandas as pd

salary = pd.Series([50000, 60000, 70000, 80000], name="salary")
print(salary)
print(type(salary))
Output
0    50000
1    60000
2    70000
3    80000
Name: salary, dtype: int64
<class 'pandas.core.series.Series'>
A Series is like a single column of data.
A DataFrame is like a full table made of multiple Series.
04 · Creating a DataFrame File · Pandas DataFrame · 4 of 12

Different Ways to Create a DataFrame

We can create a DataFrame in several ways:

  • From a Dictionary
  • From a List of Lists
  • From a List of Dictionaries
  • By reading external files (CSV, Excel, SQL, etc.)
05 · From Dictionary File · Pandas DataFrame · 5 of 12

Create DataFrame from Dictionary

Python
data = {
    "Name": ["Arun", "Priya", "Rahul", "Sneha"],
    "Age": [25, 28, 22, 30],
    "Salary": [50000, 60000, 45000, 70000]
}

df = pd.DataFrame(data)
print(df)
Output
    Name  Age  Salary
0   Arun   25   50000
1  Priya   28   60000
2  Rahul   22   45000
3  Sneha   30   70000
Each key becomes a column name, and the list of values becomes the column data.
06 · From List of Lists File · Pandas DataFrame · 6 of 12

Create DataFrame from List of Lists

Python
data = [
    ["Arun", 25, 50000],
    ["Priya", 28, 60000],
    ["Rahul", 22, 45000],
    ["Sneha", 30, 70000]
]

df = pd.DataFrame(data, columns=["Name", "Age", "Salary"])
print(df)
Output
    Name  Age  Salary
0   Arun   25   50000
1  Priya   28   60000
2  Rahul   22   45000
3  Sneha   30   70000
When creating from a list of lists, we must provide the column names separately using the columns parameter.
07 · Reading CSV Files File · Pandas DataFrame · 7 of 12

Reading Data from CSV

In real projects we almost always read data from files.

Python
df = pd.read_csv("bank_market.csv")
print(df.head())
pd.read_csv() is one of the most commonly used functions in Pandas.
08 · Basic Attributes File · Pandas DataFrame · 8 of 12

Important DataFrame Attributes

Python
print(df.shape)       # (rows, columns)
print(df.columns)     # column names
print(df.dtypes)      # data type of each column
print(df.index)       # row index
print(df.size)        # total number of elements
shape tells the size of the table.
columns gives all column names.
dtypes shows the data type of every column.
09 · Viewing Data File · Pandas DataFrame · 9 of 12

Viewing Data

Python
df.head()          # first 5 rows
df.head(10)        # first 10 rows
df.tail()          # last 5 rows
df.sample(5)       # random 5 rows
df.info()          # summary of the DataFrame
df.describe()      # statistical summary
head() and tail() are the most used methods to quickly inspect data.
describe() gives statistical summary of numerical columns.
10 · Selecting Columns File · Pandas DataFrame · 10 of 12

Selecting Columns

Python
# Single column (returns Series)
df["age"]
df.age

# Multiple columns (returns DataFrame)
df[["age", "job", "balance"]]
Use double square brackets [[ ]] when you want to select more than one column.
11 · Basic Operations File · Pandas DataFrame · 11 of 12

Basic Operations

Python
# Add a new column
df["yearly_salary"] = df["Salary"] * 12

# Delete a column
df.drop("yearly_salary", axis=1, inplace=True)

# Rename columns
df.rename(columns={"Name": "Employee_Name"}, inplace=True)
axis=1 means we are working on columns.
inplace=True modifies the original DataFrame.
12 · Summary File · Pandas DataFrame · 12 of 12

Summary

  • Pandas is the most important library for data analysis in Python.
  • A DataFrame is a table (rows + columns).
  • A Series is a single column.
  • We can create DataFrames from dictionaries, lists, or files.
  • pd.read_csv() is used to load real-world data.
  • Common attributes: shape, columns, dtypes, info(), describe().
Home Pandas DataFrame

Pandas DataFrame

Learn how to create, read, view and manipulate tabular data using Pandas DataFrames.

Select any topic from the left sidebar to begin.


Start Learning →