Adding and Dropping Columns

Pandas Basics

1 min read

Published Sep 29 2025, updated Oct 24 2025


21
0
0
0

PandasPython

Adding columns

There are various ways you can add new columns to a DaraFrame.


Assign the same value to every row

import pandas as pd

df = pd.DataFrame({"A": [1, 2, 3]})
df["B"] = 10
print(df)

Output:

   A B
0 1 10
1 2 10
2 3 10



Assigning a list or array

  • Assign different values per row (length must match number of rows).
df["C"] = [100, 200, 300]
print(df)

Output:

   A B C
0 1 10 100
1 2 10 200
2 3 10 300



Using calculations on existing columns

  • New columns can be derived from existing ones.
  • Changes the original DataFrame.
df["D"] = df["A"] + df["C"]
print(df)

Output:

   A B C D
0 1 10 100 101
1 2 10 200 202
2 3 10 300 303



Using .assign()

  • Creates a new column (or multiple) without modifying the original.
  • Can also be chained together.
df = df.assign(E=df["D"] * 2)
print(df)



Using .insert()

  • Insert a column at a specific position.
 # insert at index 1
df.insert(1, "F", [7, 8, 9])
print(df)

Output:

   A F B C D E
0 1 7 10 100 101 202
1 2 8 10 200 202 404
2 3 9 10 300 303 606






Dropping columns

There are also various ways you can remove columns from a DataFrame.


Using .drop()

  • Drops columns by name.
df.drop("B", axis=1, inplace=True) # axis=1 for columns

Drop multiple columns::

df.drop(["C", "D"], axis=1, inplace=True)

Key parameters:

  • axis=1 → columns
  • axis=0 → rows (default)
  • inplace=True → modify the DataFrame directly



Using del

  • Deletes a single column.
del df["F"]



Using .pop()

  • Removes a column and returns it.
popped_column = df.pop("E")

print(popped_column)

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact