Cheatsheet -- Python & Pandas
Python Syntax & Operators
Section titled “Python Syntax & Operators”| Category | Operator | Description | Example |
|---|---|---|---|
| Arithmetic | + - * / | Basic Math | 10 / 2 = 5.0 |
// | Floor Division (removes decimal) | 10 // 3 = 3 | |
% | Modulus (remainder) | 10 % 3 = 1 | |
** | Exponentiation (Power) | 2 ** 3 = 8 | |
| Assignment | = | Assign value | x = 5 |
+= -= | Increment/Decrement | x += 1 | |
:= | Walrus (assign inside expression) | if (n := len(a)) > 10: | |
| Comparison | == != | Equal / Not Equal | x == 5 |
> < | Greater / Less than | x > 10 | |
| Logical | and | Both must be True | True and False -> False |
or | One must be True | True or False -> True | |
not | Inverts Boolean | not True -> False | |
| Identity | is | Same object in memory? | x is None |
| Membership | in | Exists in sequence? | 'a' in 'apple' |
Data Structures
Section titled “Data Structures”| Type | Syntax | Mutable? | Access |
|---|---|---|---|
| List | [1, 2, "a"] | Yes | x[0] |
| Tuple | (1, 2, "a") | No | x[0] |
| Dict | {'k': 'v'} | Yes | x['k'] |
| Set | {1, 2, 3} | Yes | N/A (Unordered) |
Pandas Cheat Sheet
Section titled “Pandas Cheat Sheet”1. Inspection
Section titled “1. Inspection”df.head(n) # First n rowsdf.shape # (rows, columns)df.info() # Index, Datatype, Memory infodf.describe() # Statistical summarydf.columns # List of column names2. Selection
Section titled “2. Selection”df['col'] # Returns Seriesdf[['c1', 'c2']] # Returns DataFramedf.iloc[0] # Selection by position (Integer)df.loc['index_val'] # Selection by Labeldf[df['col'] > 5] # Boolean conditional selection3. Cleaning & Manipulation
Section titled “3. Cleaning & Manipulation”df.isnull().sum() # Count nulls per columndf.dropna() # Drop rows with nullsdf.fillna(value) # Fill nullsdf.astype(dtype) # Change data typedf.sort_values(by='col') # Sortdf.groupby('col').mean() # Group and aggregate4. Combining Data
Section titled “4. Combining Data”| Method | Use Case |
|---|---|
pd.concat([df1, df2]) | Stacking dataframes vertically or horizontally. |
df1.merge(df2) | Database-style join on a common column. |
df1.join(df2) | Joining based on the Index. |
Zen of Python (abridged)
Section titled “Zen of Python (abridged)”Explicit is better than implicit. Simple is better than complex. Readability counts.