0
0
Pandasdata~5 mins

Creating new columns in Pandas

Choose your learning style9 modes available
Introduction

We create new columns to add extra information or calculations to our data table. This helps us analyze data better.

You want to calculate a new value from existing columns, like total price from quantity and price per item.
You need to label data based on conditions, like marking students as pass or fail based on scores.
You want to add a constant value to all rows, like a new column showing the data source.
You want to combine text from two columns into one, like full names from first and last names.
Syntax
Pandas
df['new_column'] = expression_using_existing_columns_or_values
Use square brackets with the new column name as a string to create or replace a column.
The expression can be a calculation, a constant, or a function applied to existing columns.
Examples
Creates a new column 'total' by multiplying 'quantity' and 'price' columns.
Pandas
df['total'] = df['quantity'] * df['price']
Adds a 'status' column with 'pass' or 'fail' based on the 'score' column.
Pandas
df['status'] = ['pass' if x >= 50 else 'fail' for x in df['score']]
Adds a new column 'constant' with the same value 100 for all rows.
Pandas
df['constant'] = 100
Combines 'first_name' and 'last_name' columns into a new 'full_name' column.
Pandas
df['full_name'] = df['first_name'] + ' ' + df['last_name']
Sample Program

This code creates a DataFrame with some sample data. Then it adds four new columns:

  • total: product of quantity and price
  • status: pass/fail based on score
  • constant: a fixed value 100
  • full_name: combined first and last names

Finally, it prints the updated DataFrame.

Pandas
import pandas as pd

data = {'quantity': [2, 5, 3], 'price': [10, 7, 12], 'score': [45, 82, 67], 'first_name': ['Anna', 'Bob', 'Cara'], 'last_name': ['Smith', 'Jones', 'Lee']}
df = pd.DataFrame(data)

df['total'] = df['quantity'] * df['price']
df['status'] = ['pass' if x >= 50 else 'fail' for x in df['score']]
df['constant'] = 100
df['full_name'] = df['first_name'] + ' ' + df['last_name']

print(df)
OutputSuccess
Important Notes

When creating new columns with conditions, list comprehensions or apply() can be used.

Assigning a constant value adds the same value to all rows.

Make sure the new column name does not conflict with existing columns unless you want to replace them.

Summary

New columns add extra information or calculations to your data.

Use df['new_column'] = ... to create or update columns.

You can use calculations, conditions, constants, or combine text to make new columns.