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:
import pandas as pd
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"
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
df.insert(loc=loc, column=column, value=value)
df