Pandas
Pandas DataFrame
What is Pandas?
Pandas is a Python library mainly used for:
- Reading Data
- Cleaning data
- Manipulating Data
- Filtering data
- Aggregating data
- Combining multiple datasets
- Performing analysis
- 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.
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.
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.
A DataFrame is like a full table made of multiple Series.
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.)
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.
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.
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.
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.
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.
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.
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.
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().
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 →
