Home About

Pandas Cheat Sheet

April 17, 2021

Importing Pandas

import pandas as pd

Loading a CSV or JSON file

df = pd.read_csv('Transactions.csv')
df = pd.read_json('Data.csv')

Dropping a column

df = df.drop(['Balance'], axis=1)

Examining Column Data Types

df.dtypes

Date            object
Description     object
Credit         float64
Debit          float64
dtype: object

Converting a Column to Datetime

df['Date'] = pd.to_datetime(df['Date'], format="%d/%m/%Y")

Combining two columns

df['Amount'] = df.bfill(axis=1).iloc[:, 2]

Check for Nulls

df.isnan().values.any()

Set More useful table display widths

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

Extract a Regex into a new column

df['Extract'] = df['Description'].str.extract('(.*) - (Visa Purchase|Osko Payment|Internal Transfer|Receipt)')