Working with Data in Python Cheat Sheet
Working with Data in Python Cheat Sheet
Different
modes to Syntax: r (reading) w (writing) a (appending) + (updating: read/write) b (binary, otherwise text)
File opening
open files
modes Examples: with open("data.txt", "r") as file: content = file.read() print(content) with open("output.t
for specific
operations.
Syntax:
file.readlines() # reads all lines as a list
Different readline() # reads the next line as a string
methods to file.read() # reads the entire file content as a string
File reading read file
methods content in Example:
various with open("data.txt", "r") as file:
ways. lines = file.readlines()
next_line = file.readline()
content = file.read()
Syntax:
Different file.write(content) # writes a string to the file
write file.writelines(lines) # writes a list of strings to the file
File writing methods to
methods write Example:
content to a lines = ["Hello\n", "World\n"]
file. with open("output.txt", "w") as file:
file.writelines(lines)
Syntax:
Iterates
through for line in file: # Code to process each line
Iterating over each line in
lines the file Example:
using a with open("data.txt", "r") as file:
`loop`. for line in file: print(line)
Opens a Syntax:
file,
performs file = open(filename, mode) # Code that uses the file
operations, file.close()
Open() and and
close() explicitly Example:
closes the file = open("data.txt", "r")
file using content = file.read()
the close() file.close()
method.
Pandas
Package/Method Description Syntax and Code Example
Syntax:
dataframe_name = pd.read_excel("filename.xlsx")
Reads data from an Excel file and
.read_excel()
creates a DataFrame. Example:
df = pd.read_excel("data.xlsx")
Syntax:
dataframe_name.to_csv("output.csv", index=False)
.to_csv() Writes DataFrame to a CSV file.
Example:
df.to_csv("output.csv", index=False)
about:blank 1/3
3/6/25, 10:12 AM about:blank
Syntax:
dataframe_name["column_name"] # Accesses single column
dataframe_name[["column1", "column2"]] # Accesses multiple columns
Accesses a specific column using []
Access Columns
in the DataFrame. Example:
df["age"]
df[["name", "age"]]
Syntax:
dataframe_name.describe()
Generates statistics summary of
describe()
numeric columns in the DataFrame. Example:
df.describe()
Syntax:
dataframe_name.drop(["column1", "column2"], axis=1, inplace=True)
Removes specified rows or columns dataframe_name.drop(index=[row1, row2], axis=0, inplace=True)
drop() from the DataFrame. axis=1 indicates
Example:
columns. axis=0 indicates rows.
df.drop(["age", "salary"], axis=1, inplace=True) # Will drop columns
df.drop(index=[5, 10], axis=0, inplace=True) # Will drop rows
Syntax:
Syntax:
dataframe_name.duplicated()
Duplicate or repetitive values or
duplicated()
records within a data set. Example:
duplicate_rows = df[df.duplicated()]
Syntax:
filtered_df = dataframe_name[(Conditional_statements)]
Creates a new DataFrame with rows
Filter Rows
that meet specified conditions. Example:
filtered_df = df[(df["age"] > 30) & (df["salary"] < 50000)
Syntax:
Splits a DataFrame into groups based grouped = dataframe_name.groupby(by, axis=0, level=None, as_index=True,
on specified criteria, enabling sort=True, group_keys=True, squeeze=False, observed=False, dropna=True)
groupby() subsequent aggregation,
transformation, or analysis within Example:
each group.
grouped = df.groupby(["category", "region"]).agg({"sales": "sum"})
Syntax:
dataframe_name.head(n)
Displays the first n rows of the
head()
DataFrame. Example:
df.head(5)
Syntax:
import pandas as pd
Imports the Pandas library with the
Import pandas
alias pd. Example:
import pandas as pd
Syntax:
about:blank 2/3
3/6/25, 10:12 AM about:blank
Example:
merged_df = pd.merge(sales, products, on=["product_id", "category_id"])
Syntax:
print(df) # or just type df
Displays the content of the
print DataFrame Example:
DataFrame.
print(df)
df
Syntax:
dataframe_name["column_name"].replace(old_value, new_value, inplace=True)
Replaces specific values in a column
replace()
with new values. Example:
df["status"].replace("In Progress", "Active", inplace=True)
Syntax:
dataframe_name.tail(n)
Displays the last n rows of the
tail()
DataFrame. Example:
df.tail(5)
Numpy
Package/Method Description Syntax and Code Example
Syntax:
import numpy as np
Importing NumPy Imports the NumPy library.
Example:
import numpy as np
Syntax:
array_1d = np.array([list1 values]) # 1D Array
array_2d = np.array([[list1 values], [list2 values]]) # 2D Array
np.array() Creates a one or multi-dimensional array,
Example:
array_1d = np.array([1, 2, 3]) # 1D Array
array_2d = np.array([[1, 2], [3, 4]]) # 2D Array
Example:
- Calculates the mean of array elements
- Calculates the sum of array elements np.mean(array)
np.sum(array)
Numpy Array Attributes - Finds the minimum value in the array np.min(array
- Finds the maximum value in the array np.max(array)
- Computes dot product of two arrays np.dot(array_1, array_2)
about:blank 3/3