Skip to content
Learn Netverks

Lesson

Step 14/36 39% through track

adding-columns

Adding and transforming columns

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Adding and transforming columns: Pandas tabular manipulation—indexing, dtypes, reshaping, and analysis habits for real-world tables.

Teams apply Adding and transforming columns in every serious Pandas project—skipping it leaves blind spots in analysis and reviews.

You will apply Adding and transforming columns in contexts like: CSV/Parquet analysis, ETL notebooks, and ad hoc reporting.

Read the narrative, run `import pandas as pd` snippets with in-memory DataFrames (install pandas and numpy with pip if needed), inspect `.head()`, `.dtypes`, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Create columns from existing data: arithmetic, string ops, conditional logic with np.where, or vectorized functions. Prefer vectorized operations over apply when possible for speed.

Arithmetic and assign

import pandas as pd
import numpy as np

df = pd.DataFrame({'price': [10, 20], 'qty': [2, 3]})
df = df.assign(revenue=df['price'] * df['qty'])
print(df)

Conditional columns

df['tier'] = np.where(df['revenue'] >= 50, 'high', 'low')

Patterns to avoid

  • Python loops over rows — slow; use vectorization
  • apply on every cell when ufuncs suffice
  • Chained assignment without loc

Important interview questions and answers

  1. Q: assign vs direct?
    A: assign returns new DataFrame and allows method chaining—cleaner pipelines.
  2. Q: np.where vs apply?
    A: np.where is vectorized and faster for simple if/else on columns.

Self-check

  1. Add a revenue column from price × qty.
  2. Create a tier column with np.where.

Tip: Prefer assign and np.where over row-wise apply for speed.

Interview prep

np.where?

Vectorized if/else for column creation—faster than apply.

Vectorize why?

Delegates to NumPy C loops—avoid Python row iteration.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • assign vs copy?
  • Vectorized new col?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump