Skip to content

Instantly share code, notes, and snippets.

@reservoirinvest
reservoirinvest / jupyter_nb_subdir_change_hack.py
Last active March 12, 2023 13:15
jupyter notebook hack within vscode to import modules from parent
# needed for Jupyter notebooks
import os
from pathlib import Path
if Path.cwd().parts[-1] == 'notebooks':
root = Path.cwd().parent
else:
root = Path.cwd()
os.chdir(root)
# Using tasks on create_task for better control of errors
# Ref: [EdgeDB](https://youtu.be/-CzqsgaXUM8?t=2279)
import asyncio
import time
from typing import Callable, Coroutine
import httpx
# Let us start by making a progress reporting async function.
@reservoirinvest
reservoirinvest / scrape_trom_url.py
Last active November 27, 2017 12:55
Scrape from URLs # pandas
########################################################
#### Scrape a table with an known index into pandas
########################################################
import pandas as pd
## Scrape a single table from an URL with table index
symlotmarginurl = "https://www.5paisa.com/5pit/spma.asp"
symlotmargin = pd.read_html(symlotmarginurl)[1] # It's the second table
@reservoirinvest
reservoirinvest / column_manipulation.py
Last active November 21, 2017 23:29
Column manipulation #pandas
# replace all values of a pandas column
df['Column Name'].replace('replacement', inplace=True)
# change a column value based on some other column values
df.loc[(df['column1'] == some_value) & (df['column2'] == some_other_value), ['column_to_change']] = new_value
# List unique values in a DataFrame column
df['Column Name'].unique()
# Convert Series datatype to numeric, changing non-numeric values to NaN
@reservoirinvest
reservoirinvest / rename_columns.py
Last active November 9, 2017 05:13
Rename column names #pandas
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# OR
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
@reservoirinvest
reservoirinvest / drop_column_in_pandas.py
Last active November 9, 2017 02:59
Drop columns in pandas #pandas
df = df.drop('column_name', 1)
# where 1 is the axis number (0 for rows and 1 for columns.)
# To delete the column without having to reassign df you can do:
df.drop('column_name', axis=1, inplace=True)
# To drop a list of columns
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
@reservoirinvest
reservoirinvest / keep_only_certain_rows.py
Last active November 9, 2017 02:55
Keep only certain rows #pandas
# for scalar values
df.loc[df['column_name'] == some_value]
# for rows whose column value is in an iterable
df.loc[df['column_name'].isin(some_values)]
# Combine multiple conditions with &:
df.loc[(df['column_name'] == some_value) & df['other_column'].isin(some_values)]
# To select rows whose column value does not equal some_value, use !=: