generates a pseudo-random number; if no argument given, returns a float between zero and one
Back
Dictionary syntax
Front
names = {Key1: Value1, Key2: Value2, ...}
names[Key1] ##Lookup or Change
Keys and Values can be of any type.
Back
np.linspace(x, y, z)
Front
creates z evenly spaced numbers starting with x and ending in y
Back
Series
Front
import pandas as pd
pd.Series[data, index]
Back
pandas_datareader
Front
from pandas_datareader import data as web
import datetime
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2017, 1, 1)
stock = web.DataReader('FB', 'av-daily', start, end, api_key=('1HTZ1QTQW0NQN2LS'))
Back
savefig()
Front
save graph to file
ex:
fig.savefig('my_picture.png', dpi=200)
Back
pd.read_excel()
Front
read from excel file
ex:
pd.read_excel('excelFile.xlsx', sheetname='Sheet1')
Back
Annual Rate of Return
Front
# mean of simple returns * 250 days
returns = (mydata/mydata.shift(1)) - 1
annual_returns = returns.mean()*250
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Back
pd.merge()
Front
merges DataFrames with identical keys
ex:
pd.merge(df1, df2, how='inner', on='key')
Back
value.counts()
Front
returns values and number of times they are in a DataFrame column
ex:
df['col2'].value_counts()
Back
apply()
Front
apply a function to DataFrame
ex:
df['col2'].apply(len) ///returns series
df['col2'].apply(lamda x: x*2) /// applies user function
Back
sort_values()
Front
sorts DataFrame
ex:
df.sort_values('col2')
Back
matplotlib import code
Front
import matplotlib.pyplot as plt
%matplotlib inline ///allows plotting within notebook
%matplotlib notebook ///allows interaction
Back
Working with Lists
Front
list.append('value') // Adds to end
list.insert(index, 'value') // Adds
del list[index] // Deletes by index
list.remove('value') // Removes by value
pop_list= list.pop() //Removes last value and reassigns
Back
while loop syntax
Front
price = 4
while price > 0:
print(price)
price = price - 1
can use break to exit
Back
Python Functions
Front
ex:
def functionName(values):
....
return x
Back
remove column/row from DataFrame
Front
COLUMN: df.drop('new', axis = 1, inplace = True)
ROW: df.drop('new', axis = 0, inplace = True)
inplace will remove for good from source