KEMBAR78
CheatSheet PandasNumpy | PDF | Text File | Filename
0% found this document useful (0 votes)
3 views9 pages

CheatSheet PandasNumpy

The document is a cheat sheet for working with data in Python, covering file operations, reading and writing files, and using libraries like Pandas and NumPy. It provides syntax and examples for various methods such as reading CSV files, accessing DataFrame columns, and performing data manipulations. Key topics include file opening modes, DataFrame operations like filtering, grouping, and merging, as well as basic NumPy array functionalities.

Uploaded by

chispitaboom53
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)
3 views9 pages

CheatSheet PandasNumpy

The document is a cheat sheet for working with data in Python, covering file operations, reading and writing files, and using libraries like Pandas and NumPy. It provides syntax and examples for various methods such as reading CSV files, accessing DataFrame columns, and performing data manipulations. Key topics include file opening modes, DataFrame operations like filtering, grouping, and merging, as well as basic NumPy array functionalities.

Uploaded by

chispitaboom53
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/ 9

8/8/25, 9:00 p.m.

about:blank

Working with Data in Python Cheat Sheet


Reading and writing files
Package/Method Description Syntax and Code Example

Syntax: r (reading) w (writing) a (appending) + (updating: read/write) b (binary, otherwise text)


Examples: with open("data.txt", "r") as file: content = file.read() print(content) with open("output.txt", "w") as f

Different
modes to
File opening
open files
modes
for specific
operations.

Syntax:
file.readlines() # reads all lines as a list
readline() # reads the next line as a string
file.read() # reads the entire file content as a string

Different
methods to
File reading read file
methods content in Example:
various
ways. with open("data.txt", "r") as file:
lines = file.readlines()
next_line = file.readline()
content = file.read()

Syntax:
file.write(content) # writes a string to the file
file.writelines(lines) # writes a list of strings to the file

Different
write
File writing methods to
methods write Example:
content to a
file. lines = ["Hello\n", "World\n"]
with open("output.txt", "w") as file:
file.writelines(lines)

Iterating over Iterates Syntax:


lines through
for line in file: # Code to process each line
each line in
the file

about:blank 1/9
8/8/25, 9:00 p.m. about:blank
using a
`loop`.

Example:

with open("data.txt", "r") as file:


for line in file: print(line)

Syntax:
file = open(filename, mode) # Code that uses the file
file.close()

Opens a
file,
performs
operations,
Open() and and
close() explicitly Example:
closes the
file using file = open("data.txt", "r")
content = file.read()
the close() file.close()
method.

Syntax:
with open(filename, mode) as file: # Code that uses the file

Opens a file
using a with
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 creates a Syntax: dataframe_name = pd.read_csv("filename.csv") Example: df =
.read_csv()
DataFrame. pd.read_csv("data.csv")

about:blank 2/9
8/8/25, 9:00 p.m. about:blank

Syntax:
dataframe_name = pd.read_excel("filename.xlsx")

Reads data from an Excel file and creates a


.read_excel()
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)

Syntax:
dataframe_name["column_name"] # Accesses single column
dataframe_name[["column1", "column2"]] # Accesses multiple columns

Access Columns Accesses a specific column using [] in the DataFrame.


Example:
df["age"]
df[["name", "age"]]

describe() Generates statistics summary of numeric columns in Syntax:


the DataFrame.
dataframe_name.describe()

about:blank 3/9
8/8/25, 9:00 p.m. about:blank

Example:

df.describe()

Syntax:
dataframe_name.drop(["column1", "column2"], axis=1, inplace=True)
dataframe_name.drop(index=[row1, row2], axis=0, inplace=True)

Removes specified rows or columns from the


drop() DataFrame. axis=1 indicates columns. axis=0
indicates rows. Example:
df.drop(["age", "salary"], axis=1, inplace=True) # Will drop columns
df.drop(index=[5, 10], axis=0, inplace=True) # Will drop rows

Syntax:
dataframe_name.dropna(axis=0, inplace=True)

Removes rows with missing NaN values from the


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

duplicated() Duplicate or repetitive values or records within a data Syntax:


set.
dataframe_name.duplicated()

about:blank 4/9
8/8/25, 9:00 p.m. about:blank

Example:

duplicate_rows = df[df.duplicated()]

Syntax:
filtered_df = dataframe_name[(Conditional_statements)]

Creates a new DataFrame with rows that meet


Filter Rows
specified conditions. Example:

filtered_df = df[(df["age"] > 30) & (df["salary"] < 50000)

Syntax:
grouped = dataframe_name.groupby(by, axis=0, level=None, as_index=True,
sort=True, group_keys=True, squeeze=False, observed=False, dropna=True)

Splits a DataFrame into groups based on specified


groupby() criteria, enabling subsequent aggregation,
transformation, or analysis within each group. Example:

grouped = df.groupby(["category", "region"]).agg({"sales": "sum"})

head() Displays the first n rows of the DataFrame. Syntax:


dataframe_name.head(n)

about:blank 5/9
8/8/25, 9:00 p.m. about:blank
Example:
df.head(5)

Syntax:
import pandas as pd

Import pandas Imports the Pandas library with the alias pd.
Example:
import pandas as pd

Syntax:
dataframe_name.info()

Provides information about the DataFrame, including


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

merge() Merges two DataFrames based on multiple common Syntax:


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

Example:
merged_df = pd.merge(sales, products, on=["product_id", "category_id"])

about:blank 6/9
8/8/25, 9:00 p.m. about:blank

Syntax:
print(df) # or just type df

print DataFrame Displays the content of the DataFrame.


Example:
print(df)
df

Syntax:
dataframe_name["column_name"].replace(old_value, new_value, inplace=True)

replace() Replaces specific values in a column with new values.


Example:
df["status"].replace("In Progress", "Active", inplace=True)

tail() Displays the last n rows of the DataFrame. Syntax:


dataframe_name.tail(n)

Example:
df.tail(5)

about:blank 7/9
8/8/25, 9:00 p.m. about:blank

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:
np.mean(array)
np.sum(array)
np.min(array
np.max(array)
- Calculates the mean of array elements np.dot(array_1, array_2)
- Calculates the sum of array elements
Numpy Array Attributes - Finds the minimum value in the array
- Finds the maximum value in the array
- Computes dot product of two arrays

about:blank 8/9
8/8/25, 9:00 p.m. about:blank
© IBM Corporation. All rights reserved.

about:blank 9/9

You might also like