fromnumpyimport*frompandasimport*#using import * brings ALL the packages classes and functions into the name space,#for large packages you can bring in only some parts by doing from [name] import [class/object]#to avoid name space conflicts you can also rename what you import#import pandas and rename itimportpandasaspd#import the Series and dataframe classesfrompandasimportSeries,DataFrame#common naming conventions. numpy is np, pandas pd, and matplotlib is pltimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotasplt

#use [x] to access the item at location x in the list#all lists start at 0print'first item',a[0]#you can also index from back by using -1 for last, -2 for "second from last" etcprint'last item',a[-1]#you can "slice" a list using : and ::print'first three items',a[:3]print'last three items',a[-3:]print'start at the 4th item',a[3:]print'the odd items',a[::2]

In [ ]:

#all lists have a length, use len(list_name) to get the number of items in it#mathematical functions can also, in general, be applied to lists if they contain numbersprint'length of list',len(a)print'largets number in list',max(a)print'smallest number',min(a)print'average',mean(a)#we can find the index of the max and min using argmax() and argmin()print'the largest number in the list is',max(a),'and is found at index:',argmax(a)

#we can itterate over n items using a for loop#a shortcut for making the list [0,...,n-1] is the function range(n)#print the numbers 0 -4foriinrange(5):printi

In [ ]:

#print the numbers 0 -4foriinrange(0,5):printi

In [ ]:

#print the a listforiina:printi

In [ ]:

#itterating over something and appending is a common way of building lists#create arrayoutput=[]#build the list holding the first 4 squares by using a for loopforiinrange(5):output.append(i**2)#**2 operator means squaredoutput

In [ ]:

#this works but is slow, a faster way to do this is to use list comprehensionoutput2=[i**2foriinrange(5)]output2

In [ ]:

#we can also put conditions in the list comprehension#build the first 10 squares for all the even numbersoutput3=[i**2foriinrange(10)ifi%2==0] # % is means modulus (remainder)
output3

In [ ]:

#the zip command lines up two lists togetherL1=[1,2,3]L2=['x','y','z']#the output is a list of tuplesprintzip(L1,L2)

In [ ]:

#if they are of different size, it gets chopped offL1=[1,2,3,4]L2=['x','y','z']#the output is a list of tuplesprintzip(L1,L2)

In [ ]:

#it is very common to itterate over lists using zipforlist1,list2inzip(L1,L2):printlist1,list2

In [ ]:

#this can also be done with list comprehensionprint[(x,y)forx,yinzip(L1,L2)]#we can also make more complex listsoutput=[(x,y,str(x)+y)forx,yinzip(L1,L2)]#itterate over our output for a nicer looking print statementforzinoutput:printz#we can also do this differentlyfora1,a2,a3inoutput:printa1,a2,a3

# DB ParametersServerName="devdb4\sql4"Database="BizIntel"# To create a temp table just add a "#" to the table name# To create a global table just add a "##" to the table nameTableName="#TableCheckTest"# pyobdc must be installedengine=create_engine('mssql+pyodbc://'+ServerName+'/'+Database)conn=engine.connect()

In [ ]:

# Required for querying tablesmetadata=MetaData(conn)## Create tabletbl=Table(TableName,metadata,Column('DateAdded',DateTime),Column('Revenue',Integer))# This actually creates a table in the sql database# checkfirst=True >> create if table does not existtbl.create(checkfirst=True)

In [ ]:

# Create data to insert into table# Create a dataframe with dates as your indexdata=[1,2,3,4,5,6,7,8,9,10]idx=date_range('1/1/2012',periods=10,freq='MS')df=DataFrame(data,index=idx,columns=['Revenue'])# Remove the index if you want to include it in the insertdf=df.reset_index()#print df

In [ ]:

# Iterate through each of the columns and insert into tableforxindf.iterrows():#print list(x[1])sql=tbl.insert(list(x[1]))conn.execute(sql)# select all form tablesql=tbl.select()result=conn.execute(sql)forrowinresult:print'Write to SQL',row

# Copy paste data into a dataframe# Make sure you first copy data before running the code# This example assumes you are copying data with the first column composed of dates# index_col=0, means the date column is in the first column# parse_dates=True, makes sure dates are converted to datetime datatypesdf=read_clipboard(index_col=0,parse_dates=True)df# This has worked for me in SQL, Excel, and LibreOffice

# Write dataframe# formatting values in exceldate_xf=easyxf(num_format_str='DD/MM/YYYY')# sets date format in Excelnum_xf=easyxf(num_format_str='#0.000000')# sets date format in Excel# Iterate through each of the columns and insert into sheetfori,(date,row)inenumerate(df.T.iteritems()):#print i, date, row[0],type(row[0]).__name__# Skip first lineifi>0:iftype(date).__name__=='Timestamp':ws0.write(i,0,date,date_xf)eliftype(date).__name__=='str':ws0.write(i,0,date)else:ws0.write(i,0,date.astype(np.float),num_xf)iftype(row[0]).__name__=='Timestamp':ws0.write(i,1,row[0].astype(np.float),date_xf)eliftype(row[0]).__name__=='str':ws0.write(i,1,row[0].astype(np.float))else:ws0.write(i,1,row[0].astype(np.float),num_xf)

In [ ]:

# Remove the index if you want to include it in the insertdf=df.reset_index()# Rename columnsdf.columns=['DateAdded','Revenue']# Add column Headersfori,colsinenumerate(df.columns):#print i, colsws0.write(0,i,cols)

In [ ]:

# Write excel file# Note: This will overwrite any other files with the same namewb.save('DFtoExcel.xls')

#by default all the plots go into the same figure. we can make a new figure by calling figure()#we can also get a refrence to the figure#WITHOUT calling figure()#a line graph:plot(x,y)#a scatter plotscatter(x,y)

#plot the data, show data points as x's, connect them with a line, make it red and kinda see through#name the dataplt.plot(x,y,'x-',color='red',alpha=.5,label='the data')#add a titleplt.title('The Title')#name the axisxlabel('x axis label')ylabel('y axis label')#the legendplt.legend(loc='best')#loc = 'best' tries to make the legend not overlap the data#turn a grid onplt.grid()#save the figure as an image#this will create a ".png" file in the file location you run the code in plt.savefig('test_fig.png')

In [ ]:

#subplots and multiple linest=linspace(1,100,100)x1=np.sin(20./(2*pi)*t)x2=np.sin(40./(2*pi)*t)x3=x1+x2figure()subplot(2,1,1)plot(t,x1,label='x1')plot(t,x2,label='x2')plot(t,x3,label='x3')legend(loc='best')subplot(2,1,2)#plot a histogram#we save the histogram data but it is not needed to do so for plotting purposesx1hist=hist(x1,normed='true',bins=25,alpha=.25,label='x1')legend(loc='best')

#create some points to plot#provided is a csv file with the lat/long of US statesimportpandasaspdfrompandasimportDataFramedata=pd.read_csv('DataFiles\\usa_lat_long.csv')#it has some duplicatesdata.head()

In [ ]:

#I just want the lat/long in a listpoints=data[['longitude','latitude']].values

In [ ]:

#transform the points into map coordinatestransform_points=[m(lng,lat)forlng,latinpoints]

In [ ]:

#do all the drawing:fig=figure(figsize=(10,10))#make a larger than default imagefig.add_subplot(1,1,1)#not strictly requiredm.drawmapboundary(fill_color='white')m.fillcontinents(color='white',lake_color='white')m.drawcoastlines(color='black',linewidth=.3)m.drawcountries(color='black',linewidth=.3)m.drawstates(color='black',linewidth=.3)#plot the points on the map. These are just regular calls to matplotlib with x,y data#you could also do this in one shot by using plot(xlist, ylist...)#or using scatter(). forx,yintransform_points:plot(x,y,'o',color='red',ms=10*rand())#plot them at random sizes#we can plot some labelstext(transform_points[7][0],transform_points[7][1],'California',fontsize=15)text(transform_points[12][0],transform_points[12][1],'Florida',fontsize=15)#draw some great circleslng1,lat1=points[12]lng2,lat2=points[7]m.drawgreatcircle(lng1,lat1,lng2,lat2,linewidth=3,color='blue',alpha=.5)

# Equality constraint are defined as follows:# This is not in the correct format: x = -2# Equation has to always be equal to zero: x + 2 = 0# i.e. {'type': 'eq', 'fun': lambda x: x[0] + 2}#-------------------------------------------------------------# Inequality contraints are defined as follows:# This is not in the correct format: 2x+y<=15# Equation has to always be greater than or equal to zero: 0 <= 15 + -2x + -y ## Contraintscons=({'type':'ineq','fun':lambdax:15+-2*x[0]+-1*x[1]},#15-2x-y>=0{'type':'ineq','fun':lambdax:20+-1*x[0]+-3*x[1]},#20-x-3y{'type':'ineq','fun':lambdax:x[0]},#x>=0{'type':'ineq','fun':lambdax:x[1]})#y>=0

In [ ]:

# STILL NOT SURE WHAT THIS MEANS## Boundsbnds=((None,None),(None,None))