DataFrame column assignment
While assigning to data can be a relatively expensive operation in pandas, assigning columns to a pd.DataFrame
is a common operation.
How to do it
Let’s create a very simple pd.DataFrame
:
df = pd.DataFrame({"col1": [1, 2, 3]})
df
col1
0 1
1 2
2 3
New columns can be assigned using the pd.DataFrame[]
operator. The simplest type of assignment can take a scalar value and broadcast it to every row of the pd.DataFrame
:
df["new_column1"] = 42
df
col1 new_column1
0 1 42
1 2 42
2 3 42
You can also assign a pd.Series
or sequence as long as the number of elements matches the number of rows in the pd.DataFrame
:
df["new_column2"] = list("abc")
df
col1 new_column1 new_column2
0 1 42 a
1 2 42 b
2 3 42 c
df["new_column3"] = pd.Series(["dog", "cat"...