Tuple, List & DictionaryTuples 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.append(3.3) = [1, 'a', 2, 'b', 3.3] ----------------------------------- Dictionaries associate keys with values. x = {'Christopher Brooks': 'brooksch@umich.edu', 'Bill Gates': 'billg@microsoft.com'} String ManipulationSplit a string with split lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-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
Manipulating Variable Scale Type df = pd.DataFrame(['A+', 'A', 'A-'], index=['excellent', 'good', 'good']) 1. Change the data type to Categorical/nominal with astype df['Grades'].astype('category') 2. Give the data a logical order with ordered flag df['Grades'].astype('category', categories=['A+', 'A', 'A-'], ordered=True) 3. Reducing raio scale to inteval scale with cut pd.cut(df['avg'],3, labels=['Small', 'Medium', 'Large']) |
Pivot table |
Cheatography
https://cheatography.com
python pandas Cheat Sheet (DRAFT) by moonkey123
This is a draft cheat sheet. It is a work in progress and is not finished yet.