Labs ICT
Pro Login

Adding & Removing Columns

Growing and shrinking your DataFrame.

Adding & Removing Columns

Let me give you the tools to modify your DataFrame structure. Adding and removing columns is something you'll do constantly when cleaning data.

Adding a New Column

The simplest way — just assign it:


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35]}
df = pd.DataFrame(data)

df['Salary'] = [50000, 60000, 75000]
print(df)
    

One line and you've got a new column. It's like adding a column in Excel, but with code.

Adding a Calculated Column

Here's where it gets powerful. Create new columns based on existing ones:


df['Bonus'] = df['Salary'] * 0.1
print(df)
    

Pandas applies the calculation to every row automatically. No loops needed.

Removing Columns

Need to drop a column? Use `drop()`:


df = df.drop('Bonus', axis=1)
print(df)
    

The `axis=1` parameter tells Pandas you're dropping a column, not a row. One thing that confused me at first was axis — think of `axis=0` as rows and `axis=1` as columns.

Try it Yourself →

Key Takeaways

  • Add columns with `df['new_col'] = values`
  • Create calculated columns using existing ones
  • Use `drop()` with `axis=1` to remove columns
  • `axis=0` is rows, `axis=1` is columns