Pandas Cheat Sheet for Data Science in Python

โšก Smart Summary

Pandas cheat sheets condense the library’s most used commands into one page. This reference groups them by task: installing, building Series and DataFrames, importing files, selecting rows, cleaning data, applying functions and exporting results.

  • ๐Ÿ”˜ Two structures: Series holds one labelled dimension, while DataFrame holds two with mixed column types.
  • โ˜‘๏ธ Import anything: read_csv, read_excel, read_sql, read_json and read_html load files into a DataFrame.
  • โœ… Precise selection: loc selects by label, iloc by position, and boolean masks filter rows by condition.
  • ๐Ÿงช Clean reliably: isnull, dropna, fillna, drop_duplicates and replace handle gaps and repeated records.
  • ๐Ÿ› ๏ธ Inspect fast: shape, info, dtypes and describe summarise structure and statistics before analysis begins.
  • โš ๏ธ Version note: pandas 3.0 enables Copy-on-Write by default, so chained assignment raises an error.

Pandas Cheat Sheet for Data Science in Python

What is a Pandas Cheat Sheet?

The Pandas library ships hundreds of functions, yet only a small subset appears in everyday work. A Pandas cheat sheet is a one-page reference listing those commands, grouped by the task they perform.

It covers the Series and DataFrame structures, importing and exporting files, selecting and ordering data, cleaning values, and applying functions. Each section below pairs a short explanation with runnable code, so the page also works as a refresher for your data analysis skills.

๐Ÿ‘‰ Download the PDF of Cheat Sheet here

How to Install and Import Pandas

Two commands set Pandas up, and both run in a terminal or a Jupyter Notebook cell.

Step 1) Install Pandas

Run this once per environment; prefix it with an exclamation mark in a notebook cell.

pip install pandas

Step 2) Import Pandas

Import the package under the alias pd, which every example here assumes.

import pandas as pd

You can now call Pandas functions to manipulate, analyse and clean data. The sections below list them in the order you normally reach for them.

Pandas Data Structures

Pandas has two data structures, Series and DataFrame. Both are labelled arrays and both can hold any data type. The only difference is dimensionality: a Series is a one-dimensional array, and a DataFrame is a two-dimensional array built from NumPy arrays underneath.

Feature Series DataFrame
Dimensions One-dimensional Two-dimensional
Holds A single labelled column Many columns, mixed dtypes
Constructor pd.Series() pd.DataFrame()

1. Series

It is a one-dimensional labelled array. It can hold any data type, and None becomes a missing value.

s = pd.Series([2, -4, 6, 3, None], index=['A', 'B', 'C', 'D', 'E'])

2. DataFrame

It is a two-dimensional labelled array. It can hold any data type and different sizes of columns.

data = {'RollNo' : [101, 102, 75, 99],
        'Name' : ['Mithlesh', 'Ram', 'Rudra', 'Mithlesh'],
        'Course' : ['Nodejs', None, 'Nodejs', 'JavaScript']
}
df = pd.DataFrame(data, columns=['RollNo', 'Name', 'Course'])
df.head()

The printable cheat sheet below collects the same commands on one page.

Printable Pandas cheat sheet listing Series and DataFrame commands

Importing Data

Pandas can read many file types straight into a DataFrame. Every reader below returns one, and read_csv() is the most used.

# Import a CSV file pd
pd.read_csv(filename)

# Import a TSV file
pd.read_table(filename)

# Import a Excel file pd
pd.read_excel(filename)

# Import a SQL table/database
pd.read_sql(query, connection_object)

# Import a JSON file
pd.read_json(json_string)

# Import a HTML file
pd.read_html(url)

# From clipboard to read_table()
pd.read_clipboard()

# From dict
pd.DataFrame(dict)

Selection

You can select elements by their location or by their index label. The same square-bracket syntax picks rows, columns and slices from both structures.

1. Series

# Accessing one element from Series
s['D']

# Accessing all elements between two given indices
s['A':'C']

# Accessing all elements from starting till given index
s[:'C']

# Accessing all elements from given index till end
s['B':]

2. DataFrame

# Accessing one column df
df['Name']

# Accessing rows from after given row
df[1:]

# Accessing till before given row
df[:1]

# Accessing rows between two given rows
df[1:2]

Selecting by Boolean Indexing and Setting

Use iloc and iat for integer positions, and loc and at for labels.

1. By Position

df.iloc[0, 1]

df.iat[0, 1]

2. By Label

df.loc[[0],  ['Name']]

3. By Label/Position

df.loc[2] # Both are same
df.iloc[2]

4. Boolean Indexing

A boolean mask keeps only rows where the condition is True. The second example adds the NOT operator ~, so it also returns values that are not above 1.

# Series s where value is > 1
s[(s > 0)]

# Series s where value is <-2 or >1
s[(s < -2) | ~(s > 1)]

# Use filter to adjust DataFrame
df[df['RollNo']>100]

# Set index a of Series s to 6
s['D'] = 10
s.head()

Data Cleaning

For Python data-cleaning cheat sheet purposes, you can perform the following operations:

  • Rename columns using the rename() method.
  • Update values using the at[] or iat[] method to access and modify specific elements.
  • Create a copy of a Series or DataFrame using the copy() method.
  • Check for NULL values using the isnull() method, and drop them using the dropna() method.
  • Check for duplicate values using the duplicated() method. Drop them using the drop_duplicates() method.
  • Replace NULL values using the fillna() method with a specified value.
  • Replace values using the replace() method.
  • Sort values using the sort_values() method.
  • Rank values using the rank() method.
# Renaming columns
df.columns = ['a','b','c']
df.head()

# Mass renaming of columns
df = df.rename(columns={'RollNo': 'ID', 'Name': 'Student_Name'})

# Or use this edit in same DataFrame instead of in copy
df.rename(columns={'RollNo': 'ID', 'Name': 'Student_Name'}, inplace=True)
df.head()

# Counting duplicates in a column
df.duplicated(subset='Name')

# Removing entire row that has duplicate in given column
df.drop_duplicates(subset=['Name'])

# You can choose which one keep - by default is first
df.drop_duplicates(subset=['Name'], keep='last')

# Checks for Null Values
s.isnull()

# Checks for non-Null Values - reverse of isnull()
s.notnull()

# Checks for Null Values df
df.isnull()

# Checks for non-Null Values - reverse of isnull()
df.notnull()

# Drops all rows that contain null values
df.dropna()

# Drops all columns that contain null values
df.dropna(axis=1)

# Replaces all null values with 'Guru99'
df.fillna('Guru99')

# Replaces all null values with the mean
s.fillna(s.mean())

# Converts the datatype of the Series to float
s.astype(float)

# Replaces all values equal to 6 with 'Six'
s.replace(6,'Six')

# Replaces all 2 with 'Two' and 6 with 'Six'
s.replace([2,6],['Two','Six'])

# Drop from rows (axis=0)
s.drop(['B',  'D'])

# Drop from columns(axis=1)
df.drop('Name', axis=1)

# Sort by labels with axis
df.sort_index()

# Sort by values with axis
df.sort_values(by='RollNo')

# Ranking entries
df.rank()

# s1 is pointing to same Series as s
s1 = s

# s_copy of s, but not pointing same Series
s_copy = s.copy()

# df1 is pointing to same DataFrame as df
df1 = s

# df_copy of df, but not pointing same DataFrame
df_copy = df.copy()

Note that s1 = s only creates a second name for the same object, never a copy โ€” that is why copy() follows it.

Retrieving Information

You can perform these operations to retrieve information:

  • Use the shape attribute to get the number of rows and columns.
  • Use the head() or tail() method to obtain the first or last few rows as a sample.
  • Use the info(), describe() or dtypes method to obtain information about the data type, count, mean, standard deviation, minimum and maximum values.
  • Use the count(), min(), max(), sum(), mean() and median() methods to obtain specific statistical information for values.
  • Use the loc[] method to obtain a row.
  • Use the groupby() method to apply the GROUP BY function to group similar values in a column of a DataFrame.

1. Basic information

# Counting all elements in Series
len(s)

# Counting all elements in DataFrame
len(df)

# Prints number of rows and columns in dataframe
df.shape

# Prints first 10 rows by default, if no value set
df.head(10)

# Prints last 10 rows by default, if no value set
df.tail(10)

# For counting non-Null values column-wise
df.count()

# For range of index df
df.index

# For name of attributes/columns
df.columns

# Index, Data Type and Memory information
df.info()

# Datatypes of each column
df.dtypes

# Summary statistics for numerical columns
df.describe()

2. Summary

# For adding all values column-wise
df.sum()

# For min column-wise
df.min()

# For max column-wise
df.max()

# For mean value in number column
df.mean()

# For median value in number column
df.median()

# Count non-Null values
s.count()

# Count non-Null values
df.count()

# Return Series of given column
df['Name'].tolist()

# Name of columns
df.columns.tolist()

# Creating subset
df[['Name', 'Course']]

# Return number of values in each group
df.groupby('Name').count()

Applying Functions

apply() pushes a function across every value. Alignment rules then decide what happens when two objects share only part of their index.

# Define function
f = lambda x: x*5

# Apply this function on given Series - For each value
s.apply(f)

# Apply this function on given DataFrame - For each value
df.apply(f)

1. Internal Data Alignment

# NA values for indices that don't overlap
s2 = pd.Series([8, -1, 4],  index=['A',  'C',  'D'])
s + s2

2. Arithmetic Operations with Fill Methods

# Fill values that don't overlap
s.add(s2, fill_value=0)

3. Filter, Sort and Group By

These functions can be used for filtering, sorting and grouping Series and DataFrame objects.

# Filter rows where column is greater than 100
df[df['RollNo']>100]

# Filter rows where 70 < column < 101
df[(df['RollNo'] > 70) & (df['RollNo'] < 101)]

# Sorts values in ascending order
s.sort_values()

# Sorts values in descending order
s.sort_values(ascending=False)

# Sorts values by RollNo in ascending order
df.sort_values('RollNo')

# Sorts values by RollNo in descending order
df.sort_values('RollNo', ascending=False)

Exporting Data

Every writer below mirrors a reader from the importing section, so a file can make the round trip without extra tooling.

# Export as a CSV file df
df.to_csv(filename)

# Export as a Excel file df
df.to_excel(filename)

# Export as a SQL table df
df.to_sql(table_name, connection_object)

# Export as a JSON file
df.to_json(filename)

# Export as a HTML table
df.to_html(filename)

# Write to the clipboard
df.to_clipboard()

Colab of Cheat Sheet

Every command here is runnable in the companion notebook: Pandas Cheat Sheet โ€“ Python for Data Science.ipynb.

FAQs

Copy-on-Write is always enabled, strings use a dedicated str dtype, and chained assignment on a filtered copy raises ChainedAssignmentError. Assign through .loc instead. Every command here still runs on pandas 3.0.

pd.merge() joins on shared keys the way a SQL join does, df.join() aligns on the index, and pd.concat() stacks frames. Use merge for keys, concat for appending.

df.pivot_table() turns long rows into a wide aggregated grid, pd.melt() reverses that, and stack() or unstack() move a level between index and columns.

pd.to_datetime() converts text into datetime64, the .dt accessor exposes year, month and weekday, and resample() aggregates by day, week or month once that column is the index.

Vectorised operations run in compiled C across the whole array, while apply() calls a Python function once per row. Swapping a row-wise apply() for a vectorised expression is often ten times faster.

AI assistants map a plain description of the result onto concrete calls, suggesting groupby, merge or pivot_table with the arguments each needs. Verify the suggestion on a small sample first.

Yes. GitHub Copilot turns a comment such as group by course and count into working Pandas code inside Jupyter. Treat the output as a draft and check row counts.

Pass dtype to read_csv(), convert repeated text columns to category, downcast numbers with pd.to_numeric, and load in pieces using chunksize. df.info() reports the real footprint.

Summarize this post with: