Eight Python Techniques for Efficient Data Analysis
This article presents eight Python data analysis techniques—including list comprehensions, lambda expressions, map/filter, NumPy arange and linspace, pandas axis handling, and DataFrame concatenation, merging, joining, applying, and pivot tables—to improve code efficiency, readability, and analytical capabilities.
The article introduces eight Python methods for data analysis that boost performance and make code more elegant.
1. List Comprehension – Instead of a verbose for‑loop, Python’s list comprehension creates a list in a single line.
x = [1, 2, 3, 4]
out = []
for item in x:
out.append(item**2)
print(out)
# vs.
x = [1, 2, 3, 4]
out = [item**2 for item in x]
print(out)2. Lambda Expressions – Anonymous one‑line functions useful for quick operations. lambda arguments: expression Example:
double = lambda x: x * 2
print(double(5)) # 103. Map and Filter – Apply a function to each element (map) or select elements by a boolean rule (filter).
Map example:
seq = [1, 2, 3, 4, 5]
result = list(map(lambda var: var * 2, seq))
print(result) # [2, 4, 6, 8, 10]Filter example:
seq = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x > 2, seq))
print(result) # [3, 4, 5]4. NumPy arange and linspace – Generate numeric sequences.
arange returns values with a given step (stop is exclusive): np.arange(3, 7, 2) # array([3, 5]) linspace creates a specified number of evenly spaced points between start and stop:
np.linspace(2.0, 3.0, num=5)
# array([2.0, 2.25, 2.5, 2.75, 3.0])5. Axis in pandas – Determines whether an operation works on rows (axis=0) or columns (axis=1). Example of dropping:
df.drop('Column A', axis=1)
df.drop('Row A', axis=0)The DataFrame shape attribute returns (rows, columns), and indexing follows the same 0‑based convention.
6. Concat, Merge and Join – DataFrame combination utilities similar to SQL operations. concat stacks DataFrames vertically or horizontally, merge joins on key columns, and join merges on index or column names.
7. pandas apply – Applies a function along a specified axis, avoiding explicit loops.
df.apply(np.sqrt) # element‑wise square root
df.apply(np.sum, axis=0) # column sums
df.apply(np.sum, axis=1) # row sums8. Pivot Tables – Create Excel‑style pivot tables from a DataFrame.
pd.pivot_table(df, index=['Manager', 'Rep'])
pd.pivot_table(df, index=['Manager', 'Rep'], values=['Price'])These techniques collectively help Python users write more concise, efficient, and powerful data‑analysis code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
