Pandas Add Multiple Columns to DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:November 28, 2023

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.


# 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.


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)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
0    Spark  22000   30days      1000
1  PySpark  25000   50days      2000
2   Hadoop  30000   40days      2500
3   Pandas  35000   35days      1500

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(df)

Yields below output.


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

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)

6. 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.

Leave a Reply

You are currently viewing Pandas Add Multiple Columns to DataFrame