Fundamentals 7 min read

Python One‑Line List Comprehensions, Lambda, Map/Filter, NumPy arange & linspace, Pandas Axis, Concat/Merge/Join, Apply, and Pivot Tables

This tutorial demonstrates how to create lists with a single line of Python code, use lambda expressions, combine them with map and filter, generate numeric sequences with NumPy's arange and linspace, manipulate DataFrames using axis, concat, merge, join, apply functions, and build pivot tables for data analysis.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python One‑Line List Comprehensions, Lambda, Map/Filter, NumPy arange & linspace, Pandas Axis, Concat/Merge/Join, Apply, and Pivot Tables

Define a list in one line using list comprehension instead of a verbose for‑loop.

x = [1,2,3,4]
out = []
for item in x:
  out.append(item**2)
print(out)
# [1, 4, 9, 16]

x = [1,2,3,4]
out = [item**2 for item in x]
print(out)
# [1, 4, 9, 16]

Lambda expressions provide a concise way to create anonymous functions. lambda arguments: expression Example: a simple doubling function.

double = lambda x: x * 2
print(double(5))
# 10

Combine lambda with map to apply an operation to every element of a sequence.

# Map
seq = [1, 2, 3, 4, 5]
result = list(map(lambda var: var*2, seq))
print(result)
# [2, 4, 6, 8, 10]

Use filter with a lambda to keep elements that satisfy a condition.

# Filter
seq = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x > 2, seq))
print(result)
# [3, 4, 5]

NumPy's arange creates an arithmetic sequence with a given step.

# np.arange(start, stop, step)
np.arange(3, 7, 2)
# array([3, 5])

NumPy's linspace generates a specified number of evenly spaced points between start and stop.

# np.linspace(start, stop, num)
np.linspace(2.0, 3.0, num=5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])

In pandas, the axis parameter controls whether operations act on rows (0) or columns (1); for example, dropping a column or a row.

df.drop('Column A', axis=1)
df.drop('Row A', axis=0)

The shape attribute returns a tuple (rows, columns), helping you understand the DataFrame dimensions. df.shape # (number_of_rows, number_of_columns) DataFrames can be combined using concat (stack vertically or horizontally), merge (SQL‑style join on key columns), or join (join on index or column names).

Pandas apply lets you run a function on each element of a Series or across an axis of a DataFrame, avoiding explicit loops.

df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B'])
df.apply(np.sqrt)
#      A    B
# 0  2.0  3.0
# 1  2.0  3.0
# 2  2.0  3.0

df.apply(np.sum, axis=0)
# A    12
# B    27

df.apply(np.sum, axis=1)
# 0    13
# 1    13
# 2    13

Pandas also provides pivot_table to create Excel‑like pivot tables for summarizing data.

pd.pivot_table(df, index=["Manager", "Rep"])
pd.pivot_table(df, index=["Manager", "Rep"], values=["Price"])

These examples illustrate essential Python and pandas techniques for efficient data manipulation and analysis.

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.

LambdapandasNumPylist-comprehensionmap-filter
Python Programming Learning Circle
Written by

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.

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.