In this post, we are going to understand how to add one or multiple columns to Pandas dataframe by using the [] operator and built-in methods assign(), insert() method with the help of examples.
1. Using [] operator to Add a column to DataFrame
In this example we are adding new ‘city’ column Using [] operator in dataframe.To Add column to DataFrame Using [] operator.we pass column name between [] operator and assign list of column values the code for this is df[‘city’] = [‘WA’, ‘CA’,’NY’]
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
df['city'] = ['WA', 'CA','NY']
print(df)
Output
Name Marks Subject city
0 Jack 100 Math WA
1 Rack 98 Math CA
2 Max 99 Math NY
2. Add column by a dictionary in dataframe
In this example, we are adding a column city by using the dict() and zip() method. The list of city values is the zip to the existing column ‘Name’ to create a dictionary.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
citys = ['WA', 'CA','NY']
df['city'] = dict(zip(citys, df['Name']))
print(df)
Output
Name Marks Subject city
0 Jack 100 Math WA
1 Rack 98 Math CA
2 Max 99 Math NY
3. add a column with same default value
To add a column with a default value in each row. Let us understand with example how to achieve this. In this example, we have added an age column with a default value of 20 in each row.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
df['age'] = 20
print(df)
Output
Name Marks Subject age
0 Jack 100 Math 20
1 Rack 98 Math 20
2 Max 99 Math 20
4. insert() method to insert column in between columns
Sometimes we want to add a new in-between another column instead of the end. By using the insert() method a new column can be added.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
df.insert(2, "city", ['WA', 'CA','NY'], True)
print(df)
Output
Name Marks city Subject
0 Jack 100 WA Math
1 Rack 98 CA Math
2 Max 99 NY Math
5. Add column to Dataframe using assign() method
The assign() function assigns a new column to the dataframe and returns a new object with all the existing columns in addition to the new one. if existing columns is re-assign will be overwritten.
Syntax
dataframe.assign(***kwargs)
Parameters
***kwargs: The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
result_df = df.assign(city = ['WA', 'CA','NY'])
print(result_df)
Output
Name Marks Subject city
0 Jack 100 Math WA
1 Rack 98 Math CA
2 Max 99 Math NY
6.assign() method to Add mutiple columns
In this example, we are using the assign() method to add multiple columns city, age, grade to the dataframe.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
result_df = df.assign(city = ['WA', 'CA','NY'],Age=[18,18,20],grade=['A','B','B'])
print(result_df)
Output
Name Marks Subject city Age grade
0 Jack 100 Math WA 18 A
1 Rack 98 Math CA 18 B
2 Max 99 Math NY 20 B
7. Add new column based on value of other column
In this example, we are adding the ‘grade’ column based on the ‘Marks’ column value.We have to define a custom function add_column(df) that accepts a dataframe as an argument. In this, we are checking condition where condition marks == 100 then the grade is ‘A’ and else ‘B’
Program Example
import pandas as pd
import numpy as np
def add_column(df):
res_df = df.assign(grade=np.where(df['Marks']== 100, 'A', 'B'))
return res_df
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,98,99],
'Subject': ['Math','Math','Math']
}
df = pd.DataFrame(Student_dict)
result_df = add_column(df)
print(result_df)
Output
Name Marks Subject grade
0 Jack 100 Math A
1 Rack 98 Math B
2 Max 99 Math B
Summary
In this post we have learned multiple ways of how to add one or multiple columns to Pandas DataFrame using [] operator, assign(), insert() method with example