Pandas Cheat Sheet in Python for Data Science: Complete List for 2025
By Rohit Sharma
Updated on Jan 13, 2025 | 22 min read | 10.3k views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Jan 13, 2025 | 22 min read | 10.3k views
Share:
Table of Contents
Widely used in data science, Pandas simplifies handling structured data with its robust functions and intuitive syntax. This Pandas cheat sheet provides a comprehensive overview of commonly used commands and techniques, helping you streamline your workflow and boost efficiency.
A Pandas cheat sheet is an essential tool for anyone working with data. It bridges the gap between theory and practice by offering quick access to key commands, improving efficiency and accuracy.
Benefits of a Pandas Cheat Sheet:
With a cheat sheet, tasks like data cleaning, transformation, and statistics become more manageable, enabling you to tackle real-world challenges effectively.
Also Read: Mastering Pandas: Important Pandas Functions For Your Next Project
Incorporating a Python Pandas cheat sheet into your workflow can significantly enhance your productivity. Let us now have a look at how this sheet can be used.
The Python Pandas cheat sheet can quickly become an asset for you while handling data analytics band statistics in Python. In this section, let’s cover the major ‘cheats’ that you will frequently use while importing, exporting, cleaning, manipulating, and summarizing data.
Throughout this cheat sheet, you’ll encounter shorthand abbreviations, including:
These abbreviations make it easier to follow along with the commands and operations.
To make the most of this Python Pandas cheat sheet, it’s important to explore specific operations that you’ll frequently use in data analysis. Let’s break down the core operations you can perform using Pandas with detailed explanations and code examples.
You’ll often need to load data from various sources, and Pandas provides a variety of functions to import data into a DataFrame.
import pandas as pd
df = pd.read_csv('file_path.csv')
df = pd.read_excel('file_path.xlsx', sheet_name='Sheet1')
import sqlite3
conn = sqlite3.connect('database.db')
df = pd.read_sql_query('SELECT * FROM table_name', conn)
df = pd.read_json('file_path.json')
Also Read: How to Open json File in Excel
Master data manipulation for free with upGrad's Advanced SQL: Functions and Formulas free course! Perfect your SQL skills to complement Python and Pandas.
These commands allow you to easily load data from a variety of sources, enabling seamless integration into your Pandas workflow. Let’s now have a look at various export commands.
After analyzing or manipulating your data, you’ll need to export it back into a file format for sharing or storage. Pandas provides several ways to export your DataFrame.
df.to_csv('file_path.csv', index=False) # Excludes the index column
df.to_excel('file_path.xlsx', index=False)
df.to_json('file_path.json')
df.to_sql('table_name', conn, if_exists='replace', index=False)
Explanation:
Also Read: Data Frames in Python: Python In-depth Tutorial
These export functions help you store your results in various formats, allowing for further use or sharing with others.
Now, here are some ways in which you can inspect particular sections of series/DataFrames.
Before diving deep into analysis, it’s essential to understand your data’s structure and content. Pandas offers several methods to inspect your DataFrame or Series:
df.head() # By default, shows the first 5 rows
View the last few rows:
Similarly, you can view the last few rows:
df.tail() # By default, shows the last 5 rows
df.info()
What It Shows: The structure of your DataFrame, column data types, and the count of non-null values for each column.
df.describe()
What It Shows:
These commands allow you to quickly inspect and understand the data structure, which is a key step before performing a deeper analysis.
Analyze data like a pro for free with upGrad's free Introduction to Data Analysis using Excel course! Build a strong base to integrate with Python and Pandas.
Now that you know how to inspect your data, let's look at how to select specific parts of it for detailed analysis.
Selecting a specific subset of data is an important part of data analysis. Here’s how you can select subsets from a DataFrame:
df[['column1', 'column2']] # Selects multiple columns
df.iloc[0] # Selects the first row by index
df[df['column_name'] > 50] # Selects rows where column values are greater than 50
Example:
Suppose you have the following DataFrame:
import pandas as pd
data = {'Name': ['Amit', 'Rohit', 'Neha', 'Priya'],
'Age': [24, 35, 19, 42],
'Score': [88, 76, 95, 67]}
df = pd.DataFrame(data)
print(df)
Input DataFrame:
Name Age Score
0 Amit 24 88
1 Rohit 35 76
2 Neha 19 95
3 Priya 42 67
Apply Conditional Selection:
# Select rows where Score is greater than 80
filtered_df = df[df['Score'] > 80]
print(filtered_df)
Filtered DataFrame:
Name Age Score
0 Amit 24 88
2 Neha 19 95
These selection techniques are essential for focusing on relevant data during your analysis.
Also Read: Top 19 Python Tools Every Python Developer Should Know About
After selecting specific subsets, it’s important to clean your data before analysis. Here's how you can do it.
Data cleaning ensures that your data is in the best shape for analysis. Here are some common commands for cleaning your DataFrame:
df.dropna(inplace=True)
df.fillna(0, inplace=True)
df.drop_duplicates(inplace=True)
Rename columns:
To rename one or more columns in your DataFrame:
df.rename(columns={'old_name': 'new_name'}, inplace=True)
These data cleaning commands help ensure your data is tidy and ready for further analysis.
Also Read: A Comprehensive Guide to the Data Science Life Cycle: Key Phases, Challenges, and Future Insights
With your data cleaned, it's time to aggregate and organize it using groupby, sorting, and filtering commands.
Grouping, sorting, and filtering your data enables more focused analysis. Below are the most commonly used commands for these operations:
df.groupby('column_name').mean() # Groups by column and calculates the mean for each group
df.sort_values(by='column_name', ascending=False)
df[df['column_name'] > 50]
Once your data is grouped, sorted, and filtered, you can enhance your workflow by chaining methods together.
Example:
Suppose you have a sales dataset:
import pandas as pd
data = {'Region': ['North', 'South', 'North', 'East', 'South'],
'Sales': [200, 150, 300, 400, 250],
'Profit': [50, 30, 70, 100, 60]}
df = pd.DataFrame(data)
Input DataFrame:
Region Sales Profit
0 North 200 50
1 South 150 30
2 North 300 70
3 East 400 100
4 South 250 60
Group by Region and calculate mean sales and profit:
grouped_df = df.groupby('Region').mean()
print(grouped_df)
Output:
Sales Profit
Region
East 400.0 100.0
North 250.0 60.0
South 200.0 45.0
Use Case: Analyze average sales and profit per region to identify high-performing areas.
Method chaining allows you to apply multiple operations in one line, improving the readability and efficiency of your code.
Example:
df.dropna().sort_values(by='column_name').reset_index(drop=True)
This example demonstrates how to drop missing values, sort the data, and reset the index, all in a single line.
After chaining methods together, summarizing your data will give you a high-level view of its key statistics.
Summarizing your data helps you quickly understand its key features. Use the following commands to get a summary of your data:
df['column_name'].mean() # Mean
df['column_name'].median() # Median
df['column_name'].mode() # Mode
df.describe() # Provides a summary for numerical columns
Code your way to success for free with upGrad's free Learn Basic Python Programming course! Start with Python and unlock the world of data analysis with Pandas.
Once you’ve summarized your data, reshaping it will allow for better organization and easier analysis.
Reshaping your data can help prepare it for analysis or visualization. Pandas provides several methods for reshaping:
pd.melt(df, id_vars=['column1'], value_vars=['column2', 'column3'])
df.pivot(index='column1', columns='column2', values='column3')
After reshaping your data, you can handle missing values to ensure completeness before proceeding further.
Handling missing data is an essential part of data cleaning. Here are some common methods:
df.dropna()
df.fillna(value=0)
Now that your data is complete, it's time to create new columns that will help with further analysis.
Creating new columns based on existing data can enhance your analysis:
df['new_column'] = df['column1'] + df['column2']
Once new columns are made, visualizing your data through plotting can help identify patterns and insights.
Pandas make it easy to visualize your data:
df['column_name'].plot(kind='line')
df['column_name'].plot(kind='hist')
With your data visualized, combining different datasets can provide a broader perspective for analysis.
Combine multiple DataFrames for more comprehensive analysis:
pd.concat([df1, df2], axis=0)
pd.merge(df1, df2, on='column_name')
Finally, you can refine your queries and analyze subsets of data more efficiently using the query method.
The query() method allows you to select rows based on a condition:
df.query('column_name > 50')
This Pandas cheat sheet equips you with the key commands and functions to handle data efficiently, from importing and cleaning to analyzing and visualizing.
Example:
Filter rows where Score > 80 and Age < 30:
complex_filter = df.query('Score > 80 and Age < 30')
print(complex_filter)
Output:
Name Age Score
0 Amit 24 88
2 Neha 19 95
Use Case: Identify younger individuals with high scores for targeted interventions.
Advantages of query()
Readability: Offers a more intuitive syntax compared to traditional filtering methods.
# Traditional
df[(df['Score'] > 80) & (df['Age'] < 30)]
# Using query
df.query('Score > 80 and Age < 30')
Performance: Slightly faster for large datasets as it avoids intermediate DataFrame creation.
Also Read: Data Analysis Using Python [Everything You Need to Know]
Pandas also provide commands for advanced data manipulation which will be discussed next.
upGrad’s Exclusive Data Science Webinar for you –
ODE Thought Leadership Presentation
Advanced data manipulation is essential when working with complex datasets. In this section of the Python Pandas cheat sheet, you'll learn commands for combining and merging DataFrames, working with dates and times, and handling categorical data.
These commands help you refine and manipulate your data for more sophisticated analysis, improving your ability to extract meaningful insights from raw data.
Let’s dive into the most important Pandas commands for advanced data manipulation.
Combining and merging DataFrames is a common task when dealing with datasets spread across multiple tables or files. Use the following commands to combine and merge data efficiently:
pd.concat([df1, df2], axis=0)
This command stacks the DataFrames vertically (rows). If the columns in the DataFrames match, they will align automatically.
pd.concat([df1, df2], axis=1)
This command merges the DataFrames horizontally by aligning their indexes.
pd.merge(df1, df2, on='column_name')
This command merges two DataFrames based on a common column. The on parameter specifies the column to join on.
pd.merge(df1, df2, left_on='column1', right_on='column2')
When the columns you want to merge on have different names in the two DataFrames, you can specify which column from each DataFrame to join using left_on and right_on.
pd.merge(df1, df2, how='inner', on='column_name') # Default: Inner Join
pd.merge(df1, df2, how='outer', on='column_name') # Outer Join
pd.merge(df1, df2, how='left', on='column_name') # Left Join
pd.merge(df1, df2, how='right', on='column_name') # Right Join
df.set_index(['Year', 'Region']).join(other_df.set_index(['Year', 'Region']))
df['Discounted_Price'] = df.apply(lambda row: row['Price'] * 0.9 if row['Category'] == 'Sale' else row['Price'], axis=1)
Example: Add a calculated column for discounted prices after merging product details with sales.
import dask.dataframe as dd
ddf1 = dd.from_pandas(df1, npartitions=4)
ddf2 = dd.from_pandas(df2, npartitions=4)
merged_ddf = dd.merge(ddf1, ddf2, on='column_name'
Example: Merge customer and transaction data for millions of records efficiently.
These commands specify how rows from two DataFrames are combined based on matching keys.
Now that you know how to bring your data together, let's explore another vital aspect—managing time-related information.
Handling date and time data is a crucial aspect of data analysis, especially when dealing with time series data or logs. Below are key commands for working with dates and times in Pandas:
df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'] = pd.to_datetime(df['date_column'], format='%Y-%m-%d')
This ensures your date column is correctly recognized as a datetime object for manipulation.
Extract year, month, and day:
df['year'] = df['date_column'].dt.year
df['month'] = df['date_column'].dt.month
df['day'] = df['date_column'].dt.day
df['weekday'] = df['date_column'].dt.weekday
df['hour'] = df['date_column'].dt.hour
df['minute'] = df['date_column'].dt.minute
df['new_column'] = df['date_column'] + pd.DateOffset(days=5)
df['new_column'] = df['date_column'] - pd.DateOffset(weeks=2)
df['time_diff'] = df['date_column'] - df['other_date_column']
These commands help you manipulate and analyze datetime data efficiently.
Once you master time management, you’ll want to refine your categories for better data representation.
Categorical data, such as labels or categories, can be tricky to work with, but Pandas provides tools to handle them efficiently. Below are some commands to work with categorical variables.
Convert a column to a categorical type:
df['category_column'] = df['category_column'].astype('category')
df['category_column'] = pd.Categorical(df['category_column'])
df['category_column'].cat.categories
df['category_column'].cat.rename_categories(['new_cat1', 'new_cat2'], inplace=True)
df['new_category_column'] = df['category_column'].cat.codes
df['category_column'].dtype
df['category_column'] = df['category_column'].cat.reorder_categories(['new_cat2', 'new_cat1'], ordered=True)
Also Read: 4 Types of Data: Nominal, Ordinal, Discrete, Continuous
When working with complex datasets, a handy cheat sheet can save you time and effort. Once you’re familiar with the commands, it's time to focus on efficiency.
While the Pandas cheat sheet is an excellent reference tool, there are several tips and best practices that can help you make the most of it. These tips will not only boost your efficiency but also optimize your data analysis workflows.
Let's take a look at some strategies you can implement while using the Python Pandas cheat sheet to streamline your tasks.
Managing memory effectively is crucial when working with large datasets. Pandas can consume significant memory, especially when working with big DataFrames.
Here's how you can optimize memory usage:
df['column_name'] = df['column_name'].astype('category')
chunk = pd.read_csv('large_file.csv', chunksize=10000)
df.drop(columns=['unnecessary_column'], inplace=True)
Optimizing memory usage helps prevent your system from becoming slow or running out of memory when handling large datasets.
Now, here is a quick look at using vectorized operations.
Pandas is built to support vectorized operations, which allow you to apply functions to entire columns or rows at once. This is much faster than iterating over individual elements.
Here's how you can take advantage of vectorized operations:
df['new_column'] = df['column1'] + df['column2']
# Slow:
df['new_column'] = df['column1'].apply(lambda x: x*2)
# Faster:
df['new_column'] = df['column1'] * 2
Vectorized operations simplify repetitive tasks and enhance performance. With this powerful tool, the next step is knowing when and how to consult the documentation for deeper insights.
Although the Python Pandas cheat sheet provides a quick reference for Pandas commands, the official Pandas documentation is an invaluable resource for deeper understanding. Here’s how you can make the most of it:
The Pandas cheat sheet is perfect for quick reference, but consulting the official documentation will give you a deeper understanding and help you handle more advanced data manipulation tasks.
Also Read: Data Preprocessing in Machine Learning: 7 Key Steps to Follow, Strategies, & Applications
These tips can streamline your data analytics process, but challenges may still arise when working with diverse datasets. To help you navigate them effectively, here’s a quick look at common problems and their solutions.
Using a Pandas cheat sheet can present challenges like information overload and syntax confusion. Here's how to overcome them.
One of the challenges you might face when using a Pandas cheat sheet is the sheer amount of information it contains. The cheat sheet covers many Pandas commands, which can be overwhelming, especially for beginners.
With so many commands and functions to choose from, it's easy to feel lost. Here’s the solution for this issue:
Solution:
Once you manage the information overload, the next challenge is understanding contextual examples.
Another challenge when using the Pandas cheat sheet is understanding how the commands apply to your specific use case. The cheat sheet provides a list of Pandas commands, but it doesn’t always offer contextual examples or detailed explanations of when and why to use them.
A simple solution for this is as follows:
Solution:
Grasping examples in the right context can be tricky when applying new concepts. As you work through examples, you might also face hurdles in mastering the correct syntax. Here’s how to deal with this issue.
Even though the Pandas cheat sheet provides a quick overview of Pandas commands, the syntax for some functions can still be confusing, especially for more complex operations. This can lead to errors and delays when you’re working with data.
Here’s how to deal with this:
Solution:
Syntax errors are common when you're just starting out or transitioning to advanced concepts. After gaining clarity on syntax, the next step is addressing compatibility issues like version mismatches.
One challenge that users may face is dealing with version mismatches. Since Pandas is actively developed and frequently updated, commands or functions may behave differently across versions. This can cause compatibility issues when using the Pandas cheat sheet with older or newer versions of Pandas.
Some solutions for this problem include:
Solution:
import pandas as pd
print(pd.__version__)
pip install --upgrade pandas # To upgrade
pip install pandas==<version_number> # To install a specific version
Along with challenges, understanding common mistakes can help you avoid inefficiencies and optimize your use of Pandas.
When working with Pandas, being aware of potential pitfalls can save time and prevent inefficiencies. Here are some common mistakes to watch out for, along with tips to avoid them:
1. Overusing apply Instead of Vectorized Operations
Example:
# Inefficient
df['new_col'] = df['col'].apply(lambda x: x * 2)
# Efficient
df['new_col'] = df['col'] * 2
2. Ignoring Memory Usage for Large Datasets
df = pd.read_csv('data.csv', dtype={'col1': 'int32', 'col2': 'float32'})
#Use chunksize for large CSVs:
for chunk in pd.read_csv('large_data.csv', chunksize=10000):
process(chunk)
For even larger datasets, consider alternatives like Dask or PySpark.
3. Forgetting to Reset the Index
df = df.reset_index(drop=True)
4. Using append() in Loops
rows = []
for i in range(1000):
rows.append({'col1': i, 'col2': i * 2})
df = pd.DataFrame(rows)
5. Failing to Handle Missing Data Early
Solution: Address missing values upfront:
df['col'].fillna(0, inplace=True) # Replace missing values with 0
df.dropna(subset=['important_col'], inplace=True) # Drop rows with missing values in specific columns
6. Using Inefficient Filtering
# Inefficient
df_filtered = df[(df['col1'] > 50) & (df['col1'] < 100)]
# Efficient
df_filtered = df.query('50 < col1 < 100')
7. Not Leveraging read_* and to_* Functions Efficiently
df = pd.read_csv('data.csv', usecols=['col1', 'col2'], nrows=100)
Mastering Pandas starts with the right tools and resources, like this cheat sheet. To take your expertise further, structured learning can make all the difference. You can enhance your Pandas skills with upGrad’s expert-led programs, designed to help you excel in data science.
Structured learning is essential for mastering Pandas and advancing your data manipulation skills. By focusing on key concepts like data cleaning, wrangling, visualization, and analysis, you can confidently tackle real-world data challenges.
If you're looking for expert guidance, upGrad offers programs designed to help you build these skills systematically while applying them to practical problems.
Some of the major programs include:
Program Benefits:
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Reference Link:
https://pandas.pydata.org/pandas-docs/stable/
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources