Misc

Misc

  • Random Seed

    import random
    random.seed(12)
    
    random.sample([1,2,3,4,5], 2)
    #> [4,3]

Big Data

  • {{datatable}}
    • Misc

    • fread  for fast loading of large datasets

      import datatable as dt  # pip install datatable
      tps_dt = dt.fread("data/tps_september_train.csv").to_pandas()
  • For other options (e.g. Dask, Vaex, or cuDF) see this Kaggle notebook
  • {{cuDF}}
    • Docs, Beginner’s tutorial
    • Useful for extreme data sizes (e.g. 100s of billions of rows)
    • similar to pandas syntax
  • {{vaex}}
  • {{dask}}

Functions

  • Suppress warnings

    import warnings
    warnings.filterwarnings("ignore")
  • match/case

    • Similar to switch in R (See R, Base R >> Functions>> switch

    • Example (source)

      def show_options(options: dict|str, str|) -> None:
        for key, value in options.items():
          print(f"{key}: {value}")
        print()
      
      def main():
        options = {"1": "Add Item", "2": "Delete Item", "3": "View Item", "4": "Exit"}
        while True:
          show_options(options)
          selected_option = input("Choose an option: ")
      
          match selected_option:
            case "1":
              print("Add item logic here")
              # implement add item logic
            case "2":
              print("Delete item logic here")
              # implement view item logic
            case "3":
              print("View item logic here")
              # implement view item logic
            case "4":
              print("Goodbye!")
              break
            case _:
              print("Invalid option, please try again")
              continue