Eight Useful Python Techniques for Data Analysis: List Comprehensions, Lambda, Map/Filter, NumPy arange/linspace, Pandas Axis, Concat/Merge/Join, Apply, and Pivot Tables
This article presents eight practical Python data‑analysis techniques—including one‑line list definitions, lambda expressions, map and filter functions, NumPy arange/linspace, Pandas axis handling, DataFrame concatenation/merging/joining, the apply method, and pivot tables—each illustrated with clear code examples and explanations.
This article introduces eight Python data‑analysis methods that improve efficiency and code readability.
1. One‑line list definition – Instead of a verbose for‑loop, Python’s list comprehension creates a list in a single line. Example:
x = [1, 2, 3, 4]
out = []
for item in x:
out.append(item**2)
print(out) # [1, 4, 9, 16]
# vs.
x = [1, 2, 3, 4]
out = [item**2 for item in x]
print(out) # [1, 4, 9, 16]2. Lambda expressions – Anonymous functions useful for short, one‑off operations.
lambda arguments: expressionExample:
double = lambda x: x * 2
print(double(5)) # 103. Map and Filter – Apply a function to each element (map) or select elements based on a condition (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
result = list(filter(lambda x: x > 2, seq))
print(result) # [3, 4, 5]4. NumPy arange and linspace – Generate numeric sequences.
# arange(start, stop, step)
np.arange(3, 7, 2) # array([3, 5])
# linspace(start, stop, num)
np.linspace(2.0, 3.0, num=5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])5. Axis in Pandas – Axis 0 refers to rows, axis 1 to columns. Used in operations like df.drop('Column A', axis=1) or df.drop('Row A', axis=0) .
6. Concat, Merge, and Join – Combine DataFrames. concat stacks vertically or horizontally, merge joins on a key column, and join merges on index or column names.
7. Pandas apply – Apply a function along a specified axis without 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 spreadsheet‑style summaries.
# Simple pivot
pd.pivot_table(df, index=['Manager', 'Rep'])
# Pivot with values
pd.pivot_table(df, index=['Manager', 'Rep'], values=['Price'])The examples demonstrate how these constructs simplify data manipulation, visualization, and summarization in Python.
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.