Fundamentals 11 min read

12 Must‑Know NumPy & Pandas Functions to Supercharge Your Data Analysis

This article introduces twelve powerful NumPy and Pandas functions—six for each library—explaining their purpose, usage, and providing code snippets, enabling readers to perform efficient array manipulation, data filtering, aggregation, and I/O operations, with a link to the full Jupyter Notebook on GitHub.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
12 Must‑Know NumPy & Pandas Functions to Supercharge Your Data Analysis

6 Efficient NumPy Functions

NumPy is a Python extension for scientific computing that provides powerful N‑dimensional array objects, a wide range of mathematical functions, and tools for integrating C/C++ and Fortran code, as well as linear algebra, Fourier transform, and random number generation capabilities.

1. argpartition()

Finds the indices of the N largest values in an array and can be used to sort those values.

x = np.array([12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0])
index_val = np.argpartition(x, -4)[-4:]
index_val
# array([1, 8, 2, 0], dtype=int64)
np.sort(x[index_val])
# array([10, 12, 12, 16])

2. allclose()

Compares two arrays element‑wise within a tolerance and returns a boolean indicating whether they are close.

array1 = np.array([0.12, 0.17, 0.24, 0.29])
array2 = np.array([0.13, 0.19, 0.26, 0.31])
np.allclose(array1, array2, 0.1)  # False
np.allclose(array1, array2, 0.2)  # True

3. clip()

Limits the values in an array to a specified interval, clipping values outside the bounds.

x = np.array([3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0])
np.clip(x, 2, 5)
# array([3, 5, 5, 5, 2, 2, 5, 5, 2, 2, 5, 2])

4. extract()

Extracts elements from an array that satisfy a given condition, supporting logical operations.

# Random integers
array = np.random.randint(20, size=12)
# Condition: odd numbers
cond = np.mod(array, 2) == 1
np.extract(cond, array)
# Example output: array([ 1, 19, 11, 13,  3])
# Direct condition example
np.extract(((array < 3) | (array > 15)), array)
# Example output: array([0, 1, 19, 16, 18, 2])

5. where()

Returns elements (or their indices) that satisfy a condition, similar to SQL's WHERE clause.

y = np.array([1,5,6,8,1,7,3,6,9])
np.where(y > 5)
# array([2, 3, 5, 7, 8], dtype=int64)
np.where(y > 5, "Hit", "Miss")
# array(['Miss', 'Miss', 'Hit', 'Hit', 'Miss', 'Hit', 'Miss', 'Hit', 'Hit'], dtype='<U4')

6. percentile()

Computes the n‑th percentile of array elements along a specified axis.

a = np.array([1,5,6,8,1,7,3,6,9])
print("50th Percentile of a, axis = 0 :", np.percentile(a, 50, axis=0))
# 50th Percentile of a, axis = 0 : 6.0
b = np.array([[10, 7, 4], [3, 2, 1]])
print("30th Percentile of b, axis = 0 :", np.percentile(b, 30, axis=0))
# 30th Percentile of b, axis = 0 : [5.1 3.5 1.9]

6 Efficient Pandas Functions

Pandas is a Python library that offers fast, flexible, and expressive data structures for handling structured (tabular, multidimensional, heterogeneous) and time‑series data.

1. read_csv(nrows=n)

Read only a subset of rows from a large CSV file to save memory and time.

import io, requests
url = "https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/datasets/AirPassengers.csv"
s = requests.get(url).content
df = pd.read_csv(io.StringIO(s.decode('utf-8')), nrows=10, index_col=0)

2. map()

Maps each value in a Series to a new value using a function, dictionary, or another Series.

dframe = pd.DataFrame(np.random.randn(4, 3), columns=list('ABC'), index=['India', 'USA', 'China', 'Russia'])
changefn = lambda x: "%.2f" % x
dframe['A'].map(changefn)

3. apply()

Applies a user‑defined function along an axis of the DataFrame.

fn = lambda x: x.max() - x.min()
dframe.apply(fn)

4. isin()

Filters rows based on whether column values are present in a list of values.

filter1 = df["value"].isin([112])
filter2 = df["time"].isin([1949.0])
df[filter1 & filter2]

5. copy()

Creates an explicit copy of a Pandas object to avoid unintended modifications.

data = pd.Series(["India", "Pakistan", "China", "Mongolia"])
new = data.copy()
new[1] = "Changed value"
print(new)
print(data)

6. select_dtypes()

Returns a subset of DataFrame columns based on their data types.

framex = df.select_dtypes(include="float64")

Additionally, pivot_table() provides Excel‑like pivot functionality for summarizing data.

school = pd.DataFrame({
    'A': ['Jay', 'Usher', 'Nicky', 'Romero', 'Will'],
    'B': ['Masters', 'Graduate', 'Graduate', 'Masters', 'Graduate'],
    'C': [26, 22, 20, 23, 24]
})
table = pd.pivot_table(school, values='A', index=['B', 'C'], columns=['B'], aggfunc=np.sum, fill_value="Not Available")
print(table)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonfunctionspandasdata-analysisJupyter Notebook
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.