Show Menu
Cheatography

Python Cheat Sheet (DRAFT) by

Functions, Loops, Requests, Pandas, CSV, JSON, etc

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

Pandas

CSV
df = pd.read_csv('file.csv')
df.to_csv('file.csv', index=­False)
Exce
df = pd.read_excel('file.csv')
df.to_excel('file.csv', index=­False)
JSON
df = pd.read_json('file.json')
df.to_json('file.json', orient­='r­eco­rds', lines=­True)
SQL
from sqlalchemy import create_engine

engine = create_engine('sqlite:///database.db')
df = pd.rea­d_s­ql(­'ta­ble­_name', engine)
df.to_sql('table_name', engine, if_exi­sts­='r­epl­ace', index=­False)
A pandas DataFrame (df) is similar to a table in a database or an Excel spread­sheet.

Strings

R strings (raw)
path = r"C:­\pa­th­\to­\fil­e"
F strings (forma­tted)
greeting = f"Hello, {name}­!"
str.format()
"­Hello {}".f­or­mat­("Wo­rld­")
str.split()
["He­llo­", "­Wor­ld"]