This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################## | |
#### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}) | |
# OR | |
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 !=: |