Skip to content

Instantly share code, notes, and snippets.

@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:19
dataframe to excel
df.to_excel('test.xlsx', sheet_name='sheet1', index=False)
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:19
Filling NA's in one column with another
fullDf['forecast_date'].fillna(fullDf.day, inplace=True)
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:19
If else with lambda
calls_final.calls_tw=calls_final.calls_tw.map(lambda x: x if np.isfinite(x) else 0)
@04pallav
04pallav / new_gist_file_0
Created September 11, 2017 22:19
Adding days to dates
visits_wd['label_time']=visits_wd['last_date'].map(lambda x: x-relativedelta(months=goback))
df_test1['add']=pd.to_timedelta(df_test1['add'], unit='D')# convert integers to days this way
df_test1['forecast_date']=df_test1['forweek']+df_test1['add']
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:19
Day of week in python
df['date'].dt.weekday_name
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:20
Sort by index
df.sort_index(inplace=True) ####will so the df by index in place
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:20
Calculate last sunday for any given date
from datetime import date
def sun_date(input):
d = input.toordinal()
last = d
sunday = last - (last % 7)
return date.fromordinal(sunday)
# converting to ordunal help calculations
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:20
strftime/ Current Time
Today = datetime.now()
Today
##############datetime.datetime(2017, 7, 17, 17, 54, 32, 101687)
import pytz
print("#######################################################################################")
print("Step2: rates_calculations.ipynb run started at "+ str(datetime.now(pytz.timezone('America/Los_Angeles')).strftime('%d-%m-%Y %H:%M:%S')))
print("#######################################################################################")
Today = Today.strftime("%Y-%m-%d")
@04pallav
04pallav / new_gist_file_0
Created September 11, 2017 22:21
Create pdf from a series
from scipy.stats.kde import gaussian_kde
from numpy import linspace
# input data
data = train.loss
# data =randn(10000)
# this create the kernel, given an array it will estimate the probability over that values
kde = gaussian_kde( data )
# these are the values over wich your kernel will be evaluated
dist_space = linspace( min(data), max(data), 100 )
# plot the results
@04pallav
04pallav / new_gist_file.py
Created September 11, 2017 22:21
summary stat
train.info() #### will give a summary for the entire data
train.describe() #### will give a summary for continuous variables in the data