Python A-Z Cheatsheet for Data Analysts and Data Engineers
A - Arrays
- NumPy Arrays:
- [Link]([1, 2, 3]): Creates a NumPy array.
- [Link]((2, 3)): Creates an array filled with zeros.
- [Link]((2, 3)): Creates an array filled with ones.
- [Link](0, 10, 2): Creates an array with a range of values.
- [Link](0, 1, 5): Creates an array with a specified number of evenly spaced values.
B - Boolean Operations
- Comparison Operators:
- ==, !=, <, >, <=, >=: Used to compare values.
- Logical Operators:
- and, or, not: Used to combine Boolean expressions.
C - Conditional Statements
- If-Else:
if condition:
# code
elif condition:
# code
else:
# code
D - Data Frames
- Pandas DataFrames:
- [Link](data): Creates a DataFrame from data.
- [Link](): Shows the first few rows of the DataFrame.
- [Link](): Provides statistical summary.
- [Link](): Displays DataFrame information.
- [Link](columns=['column_name']): Removes specified columns.
- [Link]('column_name'): Groups data based on column values.
E - Exception Handling
- Try-Except:
try:
# code that may raise an exception
except Exception as e:
# code to handle the exception
finally:
# code that will run no matter what
F - Functions
- Defining Functions:
def func_name(params):
# code
return value
- Lambda Functions:
lambda x: x + 1: A short anonymous function.
G - Graphs and Plotting
- Matplotlib:
[Link](x, y): Plots data.
[Link](): Displays the plot.
- Seaborn:
[Link](data): Creates a heatmap.
[Link](x, y): Creates a scatter plot.
H - Handling Missing Data
- Pandas:
[Link](value): Replaces missing values with specified value.
[Link](): Removes rows with missing values.
I - Iteration
- Loops:
for item in iterable:: Iterates over an iterable object.
while condition:: Repeats as long as the condition is true.
J - JSON Handling
- JSON Operations:
[Link](file): Reads JSON data from a file.
[Link](data, file): Writes JSON data to a file.
[Link](string): Parses JSON from a string.
[Link](data): Converts Python data to a JSON string.
K - Key Libraries
- NumPy: For numerical operations.
- Pandas: For data manipulation and analysis.
- Matplotlib: For data visualization.
- Seaborn: For statistical data visualization.
- Scikit-Learn: For machine learning.
L - Lists
- Basic Operations:
[Link](item): Adds an item to the end.
[Link](item): Removes the first occurrence of an item.
[Link](): Sorts the list in place.
M - Merging Data
- Pandas Merge:
[Link](df1, df2, on='key'): Merges two DataFrames on a key.
[Link]([df1, df2]): Concatenates two DataFrames.
N - NumPy Operations
- Array Operations:
[Link](array): Calculates the mean of array elements.
[Link](array): Calculates the median.
[Link](array): Calculates the standard deviation.
[Link](array): Sums up the array elements.
O - Object-Oriented Programming
- Classes:
class ClassName:
def __init__(self, attribute):
[Link] = attribute
def method(self):
# code
P - Plotting
- Plot Types:
[Link](x, y): Creates a line plot.
[Link](x, height): Creates a bar plot.
[Link](data): Creates a histogram.
Q - Querying Data
- Pandas Query:
[Link]('column > value'): Filters DataFrame based on a condition.
[Link][]: Accesses a group of rows and columns by labels.
[Link][]: Accesses a group of rows and columns by integer position.
R - Reading/Writing Files
- CSV Files:
pd.read_csv('[Link]'): Reads CSV data into a DataFrame.
df.to_csv('[Link]'): Writes DataFrame to a CSV file.
- Excel Files:
pd.read_excel('[Link]'): Reads Excel data into a DataFrame.
df.to_excel('[Link]'): Writes DataFrame to an Excel file.
S - Statistics
- Basic Statistics:
[Link](): Calculates the mean.
[Link](): Calculates the median.
[Link](): Provides statistical summary of DataFrame.
T - Time Series
- Pandas Time Series:
pd.to_datetime(data): Converts data to datetime.
[Link]('M').mean(): Resamples time series data.
[Link](): Shifts data by a specified period.
U - User-Defined Exceptions
- Creating Exceptions:
class CustomException(Exception):
pass
V - Variables
- Scope:
global variable_name: Accesses a global variable within a function.
nonlocal variable_name: Accesses a variable from an outer scope.
W - Web Scraping
- Requests Library:
[Link]('url'): Sends a GET request to a URL.
[Link]: Retrieves the content of the response.
X - XML Handling
- XML Parsing:
[Link]('[Link]'): Parses XML from a file.
[Link](tag): Creates an XML element.
Y - YAML Handling
- PyYAML:
[Link](file, Loader=[Link]): Loads YAML data from a file.
[Link](data, file): Dumps Python data to a YAML file.
Z - Zipping Files
- Zip Files:
[Link]('[Link]', 'w'): Creates a new zip file.
[Link]('path'): Extracts all contents of the zip file.