Skip to content

Instantly share code, notes, and snippets.

import requests
from glob import glob
from bs4 import BeautifulSoup
import pandas as pd
from datetime import datetime
from time import sleep
# http://www.networkinghowtos.com/howto/common-user-agent-list/
HEADERS = ({'User-Agent':
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
def days_diff(df):
df['CohortIndex_d'] = (df['last_active_date'] - df['signup_date']).dt.days
def get_date_int(df, date_column):
year = df[date_column].dt.year
month = df[date_column].dt.month
week = df[date_column].dt.week
return year, month, week
def calc_cohorts(df, signup_date, last_active):
"""
signup_date: name of the column with the signup date
last_active: name of the column with the last login
def df_filter(df, string):
new = df.filter(regex=string)
return new
# Example
data = [{'2018_inc': 10, '2019_inc': 12, '2018_rev':23}, {'2018_inc':10, '2019_inc': 20, '2018_rev': 30}]
df = pd.DataFrame(data)
y_2018 = df_filter(df, '2018') # getting all columns with 2018 in their names
def split_columns(df, orig, one, two, separator):
"""
orig: string name of the column we want to split
one: string name of the first new column
two: string name of the second new column
separator: string - this is the character(s) that will define where to split
"""
df[[one,two]] = df[orig].str.split(separator,expand=True)
def clean_header(df):
"""
This functions removes weird characters and spaces from column names, while keeping everything lower case
"""
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '')
import glob
import pandas as pd
def mass_edit(file_prefix, folder_path=''):
"""
file_prefix: string that defines new file name
folder_path: no need to declare it. string copied from file explorer to the folder where the files are
"""
if folder_path == '':
folder_path = input('Please enter the path where the CSV files are:\n')
import glob
import pandas as pd
from time import strftime
def folder_csv_merge(file_prefix, folder_path='', memory='no'):
"""
file_prefix: if you want to add a prefix to the name of final merged file
folder_path: no need to declare it. string copied from file explorer to the folder where the files are
"""
if folder_path == '':
def run_app():
print('getting user inputs')
user_city_from = str(from_city_entry.get())
user_city_to = str(to_city_entry.get())
user_date_depart = str(departure_date_entry.get())
user_date_return = str(return_date_entry.get())
print('starting Chrome')
bot = Flight_Bot()
bot.start_kayak(user_city_from, user_city_to, user_date_depart, user_date_return)
# a proper app needs some buttons too!
button_run = tk.Button(bottom_frame, text="Start", command=run_app, bg='dark green', fg='white', relief='raised', width=10, font=('Helvetica 9 bold'))
button_run.grid(column=0, row=0, sticky='w', padx=100, pady=2)
button_close = tk.Button(bottom_frame, text="Exit", command=close_app, bg='dark red', fg='white', relief='raised', width=10, font=('Helvetica 9'))
button_close.grid(column=1, row=0, sticky='e', padx=100, pady=2)
window.mainloop()