Cheat Sheet: Working with Data in Python
Working with Data in Python Cheat Sheet
Reading and writing files| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| File opening modes | Different modes to open files for specific operations. | Syntax: r (reading) w (writing) a (appending) + (updating: read/write) b (binary, otherwise text)
|
| File reading methods | Different methods to read file content in various ways. | Syntax:
Example:
|
| File writing methods | Different write methods to write content to a file. | Syntax:
Example:
|
| Iterating over lines | Iterates through each line in the file using a `loop`. | Syntax:
Example:
|
| Open() and close() | Opens a file, performs operations, and explicitly closes the file using the close() method. | Syntax:
Example:
|
| with open() | Opens a file using a with block, ensuring automatic file closure after usage. | Syntax:
Example:
|
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| .read_csv() | Reads data from a `.CSV` file and creates a DataFrame. | Syntax: dataframe_name = pd.read_csv("filename.csv") Example: df = pd.read_csv("data.csv") |
| .read_excel() | Reads data from an Excel file and creates a DataFrame. | Syntax:
Example:
|
| .to_csv() | Writes DataFrame to a CSV file. | Syntax:
Example:
|
| Access Columns | Accesses a specific column using [] in the DataFrame. | Syntax:
Example:
|
| describe() | Generates statistics summary of numeric columns in the DataFrame. | Syntax:
Example:
|
| drop() | Removes specified rows or columns from the DataFrame. axis=1 indicates columns. axis=0 indicates rows. | Syntax:
Example:
|
| dropna() | Removes rows with missing NaN values from the DataFrame. axis=0 indicates rows. | Syntax:
Example:
|
| duplicated() | Duplicate or repetitive values or records within a data set. | Syntax:
Example:
|
| Filter Rows | Creates a new DataFrame with rows that meet specified conditions. | Syntax:
Example:
|
| groupby() | Splits a DataFrame into groups based on specified criteria, enabling subsequent aggregation, transformation, or analysis within each group. | Syntax:
Example:
|
| head() | Displays the first n rows of the DataFrame. | Syntax:
Example:
|
| Import pandas | Imports the Pandas library with the alias pd. | Syntax:
Example:
|
| info() | Provides information about the DataFrame, including data types and memory usage. | Syntax:
Example:
|
| merge() | Merges two DataFrames based on multiple common columns. | Syntax:
Example:
|
| print DataFrame | Displays the content of the DataFrame. | Syntax:
Example:
|
| replace() | Replaces specific values in a column with new values. | Syntax:
Example:
|
| tail() | Displays the last n rows of the DataFrame. | Syntax:
Example:
|
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| Importing NumPy | Imports the NumPy library. | Syntax:
Example:
|
| np.array() | Creates a one or multi-dimensional array, | Syntax:
Example:
|
| Numpy Array Attributes | - Calculates the mean of array elements - Calculates the sum of array elements - Finds the minimum value in the array - Finds the maximum value in the array - Computes dot product of two arrays | Example:
|
Comments
Post a Comment