Show Menu
Cheatography

python pandas Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Tuple, List & Dictionary

 
Tuples are an immutable data structure.
x = (1, 'a', 2, 'b')
------­---­---­---­---­---­---­---­---­-----
Lists are a mutable data structure.
x = [1, 'a', 2, 'b']
Use append to append an object to a list.
x.appe­nd(3.3) = [1, 'a', 2, 'b', 3.3]
------­---­---­---­---­---­---­---­---­-----
Dictio­naries associate keys with values.
x = {'Chri­stopher Brooks': 'brook­sch­@um­ich.edu', 'Bill Gates': 'billg­@mi­cro­sof­t.com'}

String Manipu­lation

 
Split a string with split
lastname = 'Chris­topher Arthur Hansen Brooks­'.s­plit(' ')[-1]
Splice a string with [ ]
X [-1] selects the last element of the list
x[:3] From the beginning of the string and stopping before the 3rd element.

Scales and Change data type

Ratio scale: mathem­atical operations of +-/* are all valid
Interval scale: equally spaced, but there is no true zero
Ordinal scale: the order of the units is important, but not evenly spaced out
Nominal scale: categories of data, but there is no order
Manipu­lating Variable Scale Type

df = pd.Dat­aFr­ame­(['A+', 'A', 'A-'], index=­['e­xce­llent', 'good', 'good'])
1. Change the data type to Catego­ric­al/­nominal with astype
df['Gr­ade­s'].as­typ­e('­cat­egory')
2. Give the data a logical order with ordered flag
df['Gr­ade­s'].as­typ­e('­cat­egory',
catego­rie­s=[­'A+', 'A', 'A-'], ordere­d=True)
3. Reducing raio scale to inteval scale with cut
pd.cut­(df­['a­vg'],3, labels­=['­Small', 'Medium', 'Large'])
 

Pivot table