Append Multiple Columns to pandas DataFrame in Python (Example Code)
In this Python programming tutorial you’ll learn how to append multiple new columns to a pandas DataFrame.
Preparing the Example
import pandas as pd # Load pandas library |
import pandas as pd # Load pandas library
my_df = pd.DataFrame({"A":range(101, 106), # Constructing pandas DataFrame "B":range(201, 206)}) print(my_df) # A B # 0 101 201 # 1 102 202 # 2 103 203 # 3 104 204 # 4 105 205 |
my_df = pd.DataFrame({"A":range(101, 106), # Constructing pandas DataFrame "B":range(201, 206)}) print(my_df) # A B # 0 101 201 # 1 102 202 # 2 103 203 # 3 104 204 # 4 105 205
Example: Concatenating Multiple Columns to pandas DataFrame
my_df["new1"], my_df["new2"], my_df["new3"] = ["x", "y", "z"] # Adding multiple variables print(my_df) # A B new1 new2 new3 # 0 101 201 x y z # 1 102 202 x y z # 2 103 203 x y z # 3 104 204 x y z # 4 105 205 x y z |
my_df["new1"], my_df["new2"], my_df["new3"] = ["x", "y", "z"] # Adding multiple variables print(my_df) # A B new1 new2 new3 # 0 101 201 x y z # 1 102 202 x y z # 2 103 203 x y z # 3 104 204 x y z # 4 105 205 x y z
Related Articles
Have a look at the following list of Python tutorials. They focus on topics such as numeric values, text elements, and naming data: