Contents

Python Pandas For Your Grandpa - 3.4 DataFrame Basic Operations

In this section, we’ll go over some basic DataFrame operations like how to insert or delete columns, and how to modify existing data.

Inserting new columns into an existing DataFrame is easy. For example, if you have a DataFrame like this

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'a': [2, 3, 11, 13],
    'b': ['fox', 'rabbit', 'hound', 'rabbit']
})
print(df)
##     a       b
## 0   2     fox
## 1   3  rabbit
## 2  11   hound
## 3  13  rabbit

you can insert a new column, ‘c’, using df['c'] and setting it equal to either a list, Series, NumPy array, or a scalar.

df['c'] = [1, 0, 1, 2]
print(df)
##     a       b  c
## 0   2     fox  1
## 1   3  rabbit  0
## 2  11   hound  1
## 3  13  rabbit  2

Note that you can’t use dot notation to create a new column. So you can’t do df.d = 1. You have to use square brackets, like df['d'] = 1.

df['d'] = 1
print(df)
##     a       b  c  d
## 0   2     fox  1  1
## 1   3  rabbit  0  1
## 2  11   hound  1  1
## 3  13  rabbit  2  1

You can also combine columns to create a new column. For example, you could create column ‘e’ as the sum of ‘a’ and ‘c’ like

df['e'] = df.a + df.c
print(df)
##     a       b  c  d   e
## 0   2     fox  1  1   3
## 1   3  rabbit  0  1   3
## 2  11   hound  1  1  12
## 3  13  rabbit  2  1  15

You can also create or update column values using boolean indexing. For example, we could update ‘d’ to equal 0 where ‘b’ is ‘rabbit’ by doing

df.loc[df.b == 'rabbit', 'd'] = 0
print(df)
##     a       b  c  d   e
## 0   2     fox  1  1   3
## 1   3  rabbit  0  0   3
## 2  11   hound  1  1  12
## 3  13  rabbit  2  0  15

Deleting columns is also pretty straight-forward. If you wanted to delete columns ‘a’ and ‘c’, just do

df.drop(columns=['a', 'c'], inplace=True)
print(df)
##         b  d   e
## 0     fox  1   3
## 1  rabbit  0   3
## 2   hound  1  12
## 3  rabbit  0  15

Course Curriculum

  1. Introduction
    1.1 Introduction
  2. Series
    2.1 Series Creation
    2.2 Series Basic Indexing
    2.3 Series Basic Operations
    2.4 Series Boolean Indexing
    2.5 Series Missing Values
    2.6 Series Vectorization
    2.7 Series apply()
    2.8 Series View vs Copy
    2.9 Challenge: Baby Names
    2.10 Challenge: Bees Knees
    2.11 Challenge: Car Shopping
    2.12 Challenge: Price Gouging
    2.13 Challenge: Fair Teams
  3. DataFrame
    3.1 DataFrame Creation
    3.2 DataFrame To And From CSV
    3.3 DataFrame Basic Indexing
    3.4 DataFrame Basic Operations
    3.5 DataFrame apply()
    3.6 DataFrame View vs Copy
    3.7 DataFrame merge()
    3.8 DataFrame Aggregation
    3.9 DataFrame groupby()
    3.10 Challenge: Hobbies
    3.11 Challenge: Party Time
    3.12 Challenge: Vending Machines
    3.13 Challenge: Cradle Robbers
    3.14 Challenge: Pot Holes
  4. Advanced
    4.1 Strings
    4.2 Dates And Times
    4.3 Categoricals
    4.4 MultiIndex
    4.5 DataFrame Reshaping
    4.6 Challenge: Class Transitions
    4.7 Challenge: Rose Thorn
    4.8 Challenge: Product Volumes
    4.9 Challenge: Session Groups
    4.10 Challenge: OB-GYM
  5. Final Boss
    5.1 Challenge: COVID Tracing
    5.2 Challenge: Pickle
    5.3 Challenge: TV Commercials
    5.4 Challenge: Family IQ
    5.5 Challenge: Concerts