• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Add Multiple Columns to DataFrame

In pandas you can add/append multiple columns to the existing DataFrame using assign() function, this function updates the existing DataFrame with new multiple columns. DataFrame.insert() is also used to insert multiple columns however, this function returns a new Dataframe after adding columns. In this article, I will explain several ways how to add multiple columns to a DataFrame with examples.

1. Quick Examples of Add Multiple Columns DataFrame

If you are in a hurry, below are some quick examples of how to add multiple columns to DataFrame.


# Quick examples of add multiple columns dataframe

# Examples 1: Add multiple columns to dataframe using df[]
Tutors = ['William', 'Henry', 'Michael', 'John']
Percent = ['5%','2%','4%','3%'] 
df['Tutors'] = Tutors
df['Percent'] = Percent

# Examples 2: Add multiple columns using Dataframe.assign()
df2 = df.assign(Tutors = ['William', 'Henry', 'Michael', 'John'],
              Percent = ['5%','2%','4%','3%'])

# Examples 3: Add multiple columns using Dataframe.inser()
df.insert(0, "Tutors", ['William', 'Henry', 'Michael', 'John'], True)
df.insert(4, "Percent", ['5%','2%','4%','3%'], True)

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are CoursesFeeDuration and Discount.


# Create DataFrame
import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
df = pd.DataFrame(technologies)
print("Create DataFrame:\n",df)

Yields below output.

pandas add multiple columns

2. Pandas Add Multiple Columns to a DataFrame Using df[]

Using df[] notation we can add multiple columns to Pandas DataFrame. This is the best example when we want to add a single column or multiple columns to DataFrame. Take two columns "Tutors”, and "Percent" as a list and pass them into df[] notation which will add these columns to the given DataFrame.


# Add multiple columns to a dataframe using df[]
Tutors = ['William', 'Henry', 'Michael', 'John']
Percent = ['5%','2%','4%','3%'] 
df['Tutors'] = Tutors
df['Percent'] = Percent
print("DataFrame with added columns:\n", df)

Yields below output.

pandas add multiple columns

3. Add Multiple Columns Using Dataframe.assign()

DataFrame.assign() is also used to add/append multiple columns to the Pandas DataFrame, this function returns a new DataFrame after adding multiple columns to the existing DataFrame. Now let’s add multiple columns "Tutors”, and "Percent" to the DataFrame. Using assign() we cannot modify the existing DataFrame in place instead it returns a new DataFrame after adding multiple columns.


# Add multiple columns using Dataframe.assign()
df2 = df.assign(Tutors = ['William', 'Henry', 'Michael', 'John'],
              Percent = ['5%','2%','4%','3%'])
print(df2)

Yields the same output as above.

4. Pandas Add Multiple Columns Using insert()

DataFrame.insert() function is another function to add multiple columns to Pandas DataFrame at any position. Using this you can specify the index where you would like to add multiple columns. The below example adds multiple columns at the first position (Index 0) and fifth position (Index 4).

Note that in pandas, the Index starts from zero. insert() function updates the existing DataFrame object with the new multiple columns.


# Add multiple columns using Dataframe.inser()
df.insert(0, "Tutors", ['William', 'Henry', 'Michael', 'John'], True)
df.insert(4, "Percent", ['5%','2%','4%','3%'], True)
print(df)

Yields below output.


# Output:
    Tutors  Courses    Fee Duration Percent  Discount
0  William    Spark  22000   30days      5%      1000
1    Henry  PySpark  25000   50days      2%      2000
2  Michael   Hadoop  30000   40days      4%      2500
3     John   Pandas  35000   35days      3%      1500

5. Complete Example of Adding Multiple Columns


import pandas as pd
import numpy as np
technologies= ({
    'Courses':["Spark","PySpark","Hadoop","Pandas"],
    'Fee': [22000,25000,30000,35000],
    'Duration':['30days','50days','40days','35days'],
    'Discount':[1000,2000,2500,1500]
              })
df = pd.DataFrame(technologies)
print(df)

# Add multiple columns to dataframe using df[]
Tutors = ['William', 'Henry', 'Michael', 'John']
Percent = ['5%','2%','4%','3%'] 
df['Tutors'] = Tutors
df['Percent'] = Percent
print(df)

# Add multiple columns using Dataframe.assign()
df2 = df.assign(Tutors = ['William', 'Henry', 'Michael', 'John'],
              Percent = ['5%','2%','4%','3%'])
print(df2)

# Add multiple columns using Dataframe.inser()
df.insert(0, "Tutors", ['William', 'Henry', 'Michael', 'John'], True)
df.insert(4, "Percent", ['5%','2%','4%','3%'], True)
print(df)

Frequently Asked Questions on Add Multiple Columns to DataFrame

How can I add multiple columns to a Pandas DataFrame?

You can add multiple columns to a Pandas DataFrame using various methods.
Direct Assignment: Use the df[] notation to assign values or lists to new column names.
insert() method: Utilize the insert() method to add columns at specific positions.
assign() method: The assign() method allows you to add multiple columns at once. It creates a new DataFrame with the added columns.
Concatenation: Use pd.concat() to concatenate the original DataFrame with a new DataFrame containing the additional columns.

Can I add multiple columns using the assign() method?

You can use the assign() method to add multiple columns to a Pandas DataFrame. The assign() method allows you to create a new DataFrame with additional columns, leaving the original DataFrame unchanged.

What’s the difference between direct assignment and insert() method?

Direct Assignment: Convenient for adding columns sequentially. The columns are appended at the end of the DataFrame.
insert() method: Useful when you want to specify the position for new columns. It allows you to insert columns at specific locations.

Are there other ways to add columns to a DataFrame?

There are several ways to add columns to a Pandas DataFrame. Besides the assign() method and direct assignment with df[] and the insert() method

How do I add columns with specific data types?

To add columns with specific data types to a Pandas DataFrame, you can specify the data type while assigning values to the new columns.

Conclusion

In this article, I have explained how to add/append multiple columns to the existing Pandas DataFrame by using df[], DataFrame.assing(), and DataFrame.insert() e.t.c. Also learned insert() is used to add multiple columns at any position of the DataFrame.

Happy Learning !!

Related Articles

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.