Show Menu
Cheatography

Pandas Cheat Sheet (DRAFT) by

This cheat sheet provides a concise reference guide for transitioning between NumPy and Pandas, two essential libraries in Python for data manipulation and analysis. It outlines the relationship between the two libraries and offers fundamental operations for handling data effectively.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Create Objects

Creating a NumPy array
arr = np.arr­ay([1, 2, 3, 4, 5])
Creating a Pandas DataFrame
df = pd.Dat­aFr­ame­({'A': [1, 2, 3], 'B': [4, 5, 6]})

Indexing and Selection:

Indexing and slicing NumPy array
arr[0]
Access element at index 0
arr[2:4]
Slice elements from index 2 to 4
Indexing and slicing Pandas DataFrame
df['A']
Access column 'A'
df.loc[0]
Access row at label 0
df.iloc[0]
Access row at index 0

Operations

Arithmetic operations with NumPy arrays
np.sum­(arr)
Sum of all elements
np.mea­n(arr)
Mean of all elements
Arithmetic operations with Pandas DataFrame
df.sum()
Sum of all elements (colum­n-wise)
df.mean()
Mean of all elements (colum­n-wise)

Missing Data Handling

Numpy
NumPy does not have built-in support for missing data
Handling missing data in Pandas DataFrame
df.isn­ull()
Detect missing values
df.dro­pna()
Drop rows with missing values
df.fil­lna­(value)
Fill missing values with specified value

Grouping

Grouping data in a Pandas DataFrame and calculates the Mean value for each group
df.gro­upb­y('­col­umn­_na­me'­).m­ean()
Grouping data in a Pandas DataFrame and calculates the Sum of values for each group
df.gro­upb­y('­col­umn­_na­me'­).sum()

Aggreg­ation

Aggreg­ating data in a Pandas DataFrame
df.gro­upb­y('­col­umn­_na­me'­).a­gg(­{'c­olu­mn_­to_­agg­reg­ate': 'aggre­gat­e_f­unc­tion'})
Example: Sum of 'values' column for each 'category'
df.gro­upb­y('­cat­ego­ry'­).a­gg(­{'v­alues': 'sum'})
 

Filtering

Filtering data in a Pandas DataFrame
df[df[­'co­lum­n_n­ame'] > threshold]
Example: Filtering rows where 'values' column is greater than 10
df[df[­'va­lues'] > 10]

Indexing