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.
Define a list in one line using list comprehension instead of a verbose for‑loop.
<code>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]</code>Lambda expressions provide a concise way to create anonymous functions.
<code>lambda arguments: expression</code>Example: a simple doubling function.
<code>double = lambda x: x * 2
print(double(5))
# 10</code>Combine lambda with map to apply an operation to every element of a sequence.
<code># Map
seq = [1, 2, 3, 4, 5]
result = list(map(lambda var: var*2, seq))
print(result)
# [2, 4, 6, 8, 10]</code>Use filter with a lambda to keep elements that satisfy a condition.
<code># Filter
seq = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x > 2, seq))
print(result)
# [3, 4, 5]</code>NumPy's arange creates an arithmetic sequence with a given step.
<code># np.arange(start, stop, step)
np.arange(3, 7, 2)
# array([3, 5])</code>NumPy's linspace generates a specified number of evenly spaced points between start and stop.
<code># np.linspace(start, stop, num)
np.linspace(2.0, 3.0, num=5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])</code>In pandas, the axis parameter controls whether operations act on rows (0) or columns (1); for example, dropping a column or a row.
<code>df.drop('Column A', axis=1)
df.drop('Row A', axis=0)</code>The shape attribute returns a tuple (rows, columns) , helping you understand the DataFrame dimensions.
<code>df.shape # (number_of_rows, number_of_columns)</code>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.
<code>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</code>Pandas also provides pivot_table to create Excel‑like pivot tables for summarizing data.
<code>pd.pivot_table(df, index=["Manager", "Rep"])
pd.pivot_table(df, index=["Manager", "Rep"], values=["Price"])</code>These examples illustrate essential Python and pandas techniques for efficient data manipulation and analysis.
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.