Skip to content

Latest commit

 

History

History
64 lines (42 loc) · 1.85 KB

Pandas_Insert_column.md

File metadata and controls

64 lines (42 loc) · 1.85 KB



Template request | Bug report | Generate Data Product

Tags: #pandas #column #insert #snippet #operation

Author: Florent Ravenel

Description: This notebook show how to insert column into DataFrame at specified location.

References:

Input

Import libraries

import pandas as pd

Setup Variables

  • loc: Insertion index. Must verify 0 <= loc <= len(columns).
  • column: Label of the inserted column.
  • value: String, Series, or array-like
loc = 0
column = "championship"
value = "NBA"

Model

Create dataframe

df = pd.DataFrame(
    {
        "team": ["A", "A", "A", "B", "B", "B"],
        "points": [11, 7, 8, 10, 21, 13],
        "assists": [5, 7, 7, 9, 12, 0],
        "rebounds": [5, 8, 10, 6, 6, 22],
    }
)
df

Insert column

df.insert(loc=loc, column=column, value=value)

Output

Display DataFrame

df