random Flashcards
Python: When scraping a list from a site
remember that you need to loop each list item into a new list, and not use the soup_page to save into pandas.
Pandas: To reference a df column by its index rather than its name, type
df.columns[0]
Pandas: To filter a column by partial string, type
mask = df[“Column name”].str.contains(“string”)
Selenium: To scroll to the bottom of a page, type
my_browser.execute_script(“window.scrollTo(0, document.body.scrollHeight);”)
To import SelectPercentile, type
from sklearn.feature_selection import SelectPercentile
To set the SelectPercentile percentile, type
SelectPercentile(percentile=20)
sklearn: To create a transformer that turns a column from an integer to a float
from sklearn.base import TransformerMixin
class MyTransformer(TransformerMixin):
def transform(self, X, **transform_params): X["Numeric"] = X["Numeric"].apply(lambda x: x.astype(float)) return X
def fit(self, X, y=None, **fit_params): return self
sklearn: To import gradient boosting, type
from sklearn.ensemble import GradientBoostingClassifier
Pandas: To set the columns on read_csv and on a new DataFrame use
read_csv: names=[]
DataFrame: columns=[]
pyautogui: To click somewhere based on a screentshot’s center, type
pixel_x, pixel_y = pyautogui.locateCenterOnScreen(“screenshot.png”)
pyautogui.click(pixel_x, pixel_y)
pyautogui: To have a dialogue box pop up and confirm that you want to continue, type
pyautogui.confirm(“Proceed?”)
pyautogui: To find the pixel coordinates of the current mouse position, and then click them, type
current_x, current_y = pyautogui.position()
pyautogui.click(current_x, current_y)
pyautogui: To move the mouse, type
pyautogui.moveTo(100, 150)
pyautogui: To type characters, type
pyautogui.typewrite(“My String”, interval=0.25)
pyautogui: To take a screenshot and then save it, type
screenshot = pyautogui.screenshot()
screenshot.save(“path/screenshot.png”)
smtplib: To send a gmail email with an image attachment, type
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
my_msg = MIMEMultipart()
my_msg[“Subject”] = “My subject”
my_msg.attach(MIMEText(“My body message text”, “plain”))
fp = open(“file/path.png”, ‘rb’)
file = MIMEImage(fp.read())
fp.close()
my_msg.attach(file)
server = smtplib.SMTP(“smtp.gmail.com:587”)
server. ehlo()
server. starttls()
server. login(“me@gmail.com”, “password”)
server. sendmail(“from@gmail.com”, [“to@gmail.com”], my_msg.as_string())
server. quit()
smtplib: To send a gmail email with a csv attachment, type
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMETe xt from email import encoders
my_msg = MIMEMultipart()
my_msg[“Subject”] = “My subject”
my_msg.attach(MIMEText(“My body message text”, “plain”))
fp = open(“/path/filename.csv”, “rb”)
file = MIMEBase(“application”, “octet-stream”)
file.set_payload(fp.read())
fp.close()
encoders.encode_base64(file)
file.add_header(“Content-Disposition”, “file”, filename=”filename.csv”)
my_msg.attach(file)
server = smtplib.SMTP(“smtp.gmail.com:587”)
server. starttls()
server. login(“me@gmail.com”,”password”)
server. sendmail(“from@gmail.com”, [“to@gmail.com”], my_msg.as_string())
server. quit()
Math: ROI is
revenue divided by cost
Python: To create a special method that returns a string when print is called on a class instance, type
class Myclass(Parentclass): def \_\_str\_\_(self): return "This string is returned when print(my_instance) is called"
Python: To create an __init__ method that prompts an input upon instantiation, type
class Myclass: def \_\_init\_\_(self, **args): self.my_attribute = input("Prompt string")
Python: To create an __init__ method that prompts a method that then prompts two inputs, type
class Myclass: def \_\_init\_\_(self, **args): self.my_attribute = self.input_method()
def input_meth(self): my_attribute = input("Prompt string") return my_attribute
Python: To combine two columns together so their rows are both available in every iteration of a for loop, type
for item1, item2 in tuple(zip(df[“column”].tolist(), df[“column”].tolist())):
print(item1, item2)
Python: A generator expression is
the same as a list comprehension but can be passed into a function without turning it into a list.
Python: To write a generator expression, type
(item for item in my_list if item >5)
This is the content of my body
{% endblock %}Upload new File
'''