To assign a new column to Pandas DataFrame:
df = df.assign(new_column_name=["item_1", "item_2", "item_3", ...])
Case 1: Assign a Single Column to Pandas DataFrame
To start with a simple example, create a DataFrame with a single column about products:
import pandas as pd
data = {"product_name": ["tablet", "printer", "laptop", "monitor"]}
df = pd.DataFrame(data)
print(df)
This is how the DataFrame would look like in Python:
product_name
0 tablet
1 printer
2 laptop
3 monitor
You can then use the following template to assign a new column to the DataFrame:
df = df.assign(new_column_name=["item_1", "item_2", "item_3", ...])
For example, to assign a new column, called ‘price‘, to the original DataFrame:
import pandas as pd
data = {"product_name": ["tablet", "printer", "laptop", "monitor"]}
df = pd.DataFrame(data)
df = df.assign(price=[250, 100, 1200, 300])
print(df)
Run the code in Python, and you’ll see the new column:
product_name price
0 tablet 250
1 printer 100
2 laptop 1200
3 monitor 300
Note that the number of items under your new column must match with the length of index in your existing DataFrame. In our example, since we have 4 products, you must add 4 prices under the new ‘price’ column. Otherwise, you’ll get the following error:
ValueError: Length of values does not match length of index
Case 2: Assign Multiple Columns to Pandas DataFrame
Now let’s assign two columns to the DataFrame:
- The ‘price‘ column; and
- The ‘brand‘ column
You can then apply the following code in order to assign those two columns to the DataFrame:
import pandas as pd
data = {"product_name": ["tablet", "printer", "laptop", "monitor"]}
df = pd.DataFrame(data)
df = df.assign(price=[250, 100, 1200, 300], brand=["aa", "bb", "cc", "dd"])
print(df)
Once you run the code in Python, you’ll see these two additional columns:
product_name price brand
0 tablet 250 aa
1 printer 100 bb
2 laptop 1200 cc
3 monitor 300 dd
You can read more about assigning new columns to a DataFrame by visiting the Pandas Documentation.