PANDAS
Powerful Data Analysis with Python
Comprehensive Guide: From Beginner to Advanced
WHAT IS PANDAS?
Definition
Pandas is an open-source Python library providing
high-performance, easy-to-use data structures and data
Core Purpose
analysis tools.
To make working with relational or labeled data
• Built on top of NumPy.
• Primary tool for Data Science. both easy and intuitive.
• Handles "Panel Data" (hence the name).
HISTORY AND EVOLUTION
ORIGIN EVOLUTION
Created by Wes McKinney in 2008 at AQR Capital • 2009: Became Open Source.
• 2012: Python for Data Analysis book published.
Management.
• 2015: NumFOCUS sponsored project.
• Present: The gold standard for tabular data.
WHY PANDAS IS POPULAR
Fast Flexible Community
Optimized C and Cython code. Integrates with NumPy, Massive support and
Matplotlib, Scikit-learn. documentation.
FEATURES AND ADVANTAGES
• DataFrame Object: Fast and efficient. • Slicing: Label-based indexing.
• I/O Tools: Load CSV, Excel, SQL, JSON. • Merging: High performance joins.
• Missing Data: Easy handling of NaNs. • Time Series: Native support for dates.
• Reshaping: Pivoting and melting datasets. • Aggregations: Powerful GroupBy engine.
INSTALLING PANDAS
COMMAND LINE INTERFACE
For Anaconda users:
Requires Python 3.8+ and NumPy.
IMPORTING PANDAS
Standard Alias
The alias 'pd' is a universal convention in the Data Science community. Always use it to keep your code standard and
readable.
INTRODUCTION TO SERIES
DEFINITION EXAMPLE
A 1D labeled array capable of holding any
data type.
SYNTAX
INTRODUCTION TO DATAFRAME
DEFINITION
A 2D labeled data structure with columns of potentially
different types.
Think of it as an Excel Table.
ROWS, COLUMNS, AND INDEX
Components
• Index: The Row
labels (Axis 0).
• Columns: The
Column names
(Axis 1).
• Values: The actual
data (NumPy
array).
DATAFRAME FROM DICTIONARY
METHOD
Keys become columns; Values (lists) become rows.
DATAFRAME FROM LIST OF LISTS
USE CASE
Useful when data is received row-by-row, such as results from a database query or a custom parser.
CREATING EMPTY DATAFRAME
SYNTAX
[Link](columns=['A', 'B'])
USE CASE
When you need to initialize a container and append rows dynamically within a loop (though vectorization is
preferred).
READING CSV AND EXCEL FILES
CSV EXCEL
Pandas supports 15+ formats including SQL, JSON, Parquet, and HTML tables.
INSPECTION: HEAD()
DEFINITION
Returns the first n rows of the object based on position.
SYNTAX
OUTPUT
Displays top 5 rows of our Employee Dataset.
INSPECTION: TAIL()
DEFINITION
Returns the last n rows of the object based on position.
SYNTAX
OUTPUT
Displays Diana and other bottom entries.
INSPECTION: SHAPE
DEFINITION
Returns a tuple representing the dimensionality (rows, cols).
EXAMPLE
INSPECTION: COLUMNS
DEFINITION
The column labels of the DataFrame.
EXAMPLE
INSPECTION: DTYPES
DEFINITION
Returns the data types of each column.
INSPECTION: INFO()
A concise summary of the DataFrame including non-null counts and memory usage.
INSPECTION: DESCRIBE()
STATISTICAL SUMMARY
Stats Age Salary
count 4.0 4.0
mean 29.5 58750
min 25.0 50000
QUICK STATS HELPERS
VALUE_COUNTS() UNIQUE()
IT: 2, HR: 1, Sales: 1 ['NY', 'LA', 'Chicago', 'Boston']
SINGLE COLUMN SELECTION
SYNTAX ATTRIBUTE METHOD
Returns a Series. Works if the name has no spaces and doesn't conflict with
methods.
MULTIPLE COLUMN SELECTION
SYNTAX
Returns a DataFrame.
SELECTION: LOC[]
DEFINITION
Access a group of rows and columns by labels.
Inclusive of the last element in slicing.
SELECTION: ILOC[]
DEFINITION
Integer-location based indexing for selection by position.
Strictly follows Python's zero-based integer indexing.
SINGLE CONDITION FILTERING
USE CASE
Filter employees older than 28.
MULTIPLE CONDITIONS
LOGICAL OPERATORS
Use & (AND), | (OR), ~ (NOT).
Charlie (35, IT) is the result.
ISIN() & [Link]()
ISIN() STARTSWITH()
CREATING NEW COLUMNS
EXAMPLE
Simple broadcasted arithmetic creates a column 'Bonus' with 10% of salary.
MANIPULATION: APPLY()
DEFINITION
Apply a function along an axis of the DataFrame.
MANIPULATION: ASSIGN()
DEFINITION
Assign new columns to a DataFrame, returning a new object.
Allows method chaining unlike bracket assignment.
[Link](), [Link]()
SYNTAX
[Link](), [Link]()
LEN() CONTAINS()
[Link](), [Link]()
Use expand=True in split to create multiple columns from one string.
MISSING VALUES AND FILLNA()
FILLNA() SYNTAX
Replaces NaN with the average salary of the team.
CLEANING: DROPNA()
SYNTAX
Removes any row that contains at least one null value.
subset=['Name']: Only drop if Name is missing.
CONVERSION: ASTYPE()
USE CASE
Convert Age to float or float Salary back to int.
CLEANING: TO_DATETIME()
Automatically parses many date formats (YYYY-MM-DD, DD/MM/YY, etc.). Enables the '.dt' accessor for time
operations.
RENAMING & INDEXING
RENAME() RESET_INDEX()
GROUPING: GROUPBY()
EXAMPLE
Follows the Split-Apply-Combine pattern.
AGGREGATION: AGG()
MULTIPLE AGGREGATES
Calculates total, average, and highest salary per department simultaneously.
GROUPING: TRANSFORM()
DEFINITION
Returns an object that is the same size as the input.
RANK & FILTER
FILTER() RANK()
ADVANCED: PIVOT_TABLE()
Creates a spreadsheet-style pivot table as a DataFrame.
ADVANCED: CROSSTAB()
SYNTAX
Computes a frequency table of two (or more) factors.
JOINING DATA
Supports Left, Right, Outer joins.
[Link]():
Database-style
joins.
SORTING & TOP N
SORT_VALUES() NLARGEST()
sort_index(): Sorts rows by the index labels.
TIME SERIES & DATETIME
CREATION DT ACCESSOR
• pd.to_datetime() • df['Date'].[Link] / month / day
• pd.date_range(start, end) • df['Date'].[Link]
• [Link]() • df['Date'].[Link]('%B')
[Link]: Represents duration between two dates.
SUMMARY & BEST PRACTICES
Final Export
Conclusion
Mastering Pandas is the gateway to Data
Common Mistakes
Science. Keep practicing with real datasets!
Not using inplace=True (deprecated, use assignment).
Ignoring copy() warnings.
IMAGE SOURCES
[Link]
Source: [Link]
[Link]
Source: [Link]
[Link]
Qo=
Source: [Link]
[Link]
Source: [Link]