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