Skip to content

Instantly share code, notes, and snippets.

View qzcool's full-sized avatar
🎯
Focusing

Qiao Zhang qzcool

🎯
Focusing
View GitHub Profile
@qzcool
qzcool / Download File Common Function
Last active May 3, 2018 01:59
[Python 3.6] Download File Common Function
# Download File Common Function
def download_file(url, path):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
#f.flush() commented by recommendation from J.F.Sebastian
@qzcool
qzcool / Currency Value Subtraction
Last active May 3, 2018 02:00
Subtract Numers (Int/Float) from a String
# Usage: currency value subtraction
def find_number(text):
if len(re.findall(r'\.',text)) == 0:
return float(re.findall(r'\d+',text.replace(",", ""))[0])
else:
return float(re.findall(r'\d+\.\d+',text.replace(",", ""))[0])
@qzcool
qzcool / Check Nan Function
Created May 3, 2018 02:00
Check Nan Function
# Def Check Nan Function
# https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-in-python
def is_nan(x):
return isinstance(x, float) and math.isnan(x)
@qzcool
qzcool / Get Position Function
Last active May 3, 2018 02:02
Get Position Function: 增加程序对数据源结构变化的稳定性
# Def get position function: 增加程序对数据源结构变化的稳定性
def f_p(df, feature):
feature_list = df.iloc[:,0].get_values().tolist() # 第一列为特征名称,Index为连续自然数
return [i for i,x in enumerate(feature_list) if x == feature][0]
@qzcool
qzcool / 公司标准名称清洗
Created May 3, 2018 05:08
公司标准名称清洗
from bs4 import BeautifulSoup
import requests, datetime
from tqdm import *
import pandas as pd
engine_bing = 'https://cn.bing.com/search?q=site%3Atianyancha.com+'
engine_baidu = 'https://www.baidu.com/s?wd=site%3A%20tianyancha.com%20'
def get_exact_name(name_list, name_column):
df = pd.read_excel(name_list,encoding='gb18030')
@qzcool
qzcool / Change Data Type from Obect to Datetime
Created May 21, 2018 10:59
Change Data Type from Obect to Datetime
df_cf["tradeDate"] = df_cf["tradeDate"].astype("datetime64[ns]")
@qzcool
qzcool / Get Current File Path for Migration Consistency
Last active June 14, 2018 06:45
Get current file path for migration consistency.
# Get current file path for migration consistency
path = os.getcwd().replace('\\','/') #r'%s' % os.getcwd().replace('\\','/')
@qzcool
qzcool / [Windows] Chrome Program Kill Function
Last active June 14, 2018 06:52
[Windows] program kill functions, currently including Excel and Chrome.
import psutil
# Chrome App Kill
def kill_chrome():
for proc in psutil.process_iter():
if 'chrome.exe' in proc.name():
proc.kill()
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
app = dash.Dash()
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
@qzcool
qzcool / calendar_position.py
Last active June 23, 2019 06:40
Get the calendar position of start and end date of any date range
import datetime
date_start = datetime.datetime.strptime('2019-09-26', '%Y-%m-%d').date()
date_end = date_start + datetime.timedelta(days=1)
# 每月第一天的星期日名称,并推算date_start日和T+1日的表格位置
def get_calender_position_x(date):
position_x = date.weekday() + 1
if position_x == 7:
position_x = 0
position_x += 1