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 Courses
, Fee
, Duration
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
- Pandas Add Column based on Another Column
- Pandas Add Column Names to DataFrame
- Pandas Add Column to DataFrame
- Add Column Name to Pandas Series
- Pandas Add Constant Column to DataFrame
- Pandas – Add an Empty Column to a DataFrame
- Convert Pandas Column to Lowercase
- How to Convert Pandas Uppercase Column
- Pandas Filter DataFrame by Multiple Conditions
- Select pandas columns based on condition
- Pandas Check Column Contains a Value in DataFrame
- Pandas Extract Column Value Based on Another Column
- Split the column of DataFrame into two columns
- Split Pandas DataFrame by column value
- Split pandas DataFrame
- Pandas Convert Row to Column Header in DataFrame