0% found this document useful (0 votes)
4 views

Working with Data in Python Cheat Sheet

This cheat sheet provides essential methods and syntax for working with data in Python, including reading and writing files, using the Pandas library for data manipulation, and utilizing NumPy for numerical operations. It covers file handling, DataFrame operations, and basic NumPy array functionalities. Each section includes syntax and code examples for practical application.

Uploaded by

dmediciana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Working with Data in Python Cheat Sheet

This cheat sheet provides essential methods and syntax for working with data in Python, including reading and writing files, using the Pandas library for data manipulation, and utilizing NumPy for numerical operations. It covers file handling, DataFrame operations, and basic NumPy array functionalities. Each section includes syntax and code examples for practical application.

Uploaded by

dmediciana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3/6/25, 10:12 AM about:blank

Working with Data in Python Cheat Sheet

Reading and writing files


Package/Method Description Syntax and Code Example

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.

Opens a file Syntax:


using a with with open(filename, mode) as file: # Code that uses the file
block,
with open() ensuring Example:
automatic
file closure with open("data.txt", "r") as file:
after usage. content = file.read()

Pandas
Package/Method Description Syntax and Code Example

Reads data from a `.CSV` file and


.read_csv() Syntax: dataframe_name = pd.read_csv("filename.csv") Example: df = pd.read_csv("data.csv")
creates a DataFrame.

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:

Removes rows with missing NaN dataframe_name.dropna(axis=0, inplace=True)


dropna() values from the DataFrame. axis=0
Example:
indicates rows.
df.dropna(axis=0, inplace=True)

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:

Provides information about the dataframe_name.info()


info() DataFrame, including data types and
Example:
memory usage.
df.info()

merge() Merges two DataFrames based on Syntax:


multiple common columns.
merged_df = pd.merge(df1, df2, on=["column1", "column2"])

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)

© IBM Corporation. All rights reserved.

about:blank 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy