Dataframes are often used to handle tabular data in python. In this article, we will discuss how we can insert a new column into a dataframe in python.
Insert New Column Into a Dataframe by Indexing in Python
To add a new column into a dataframe, we can use indexing in the same way we add a key-value pair in a python dictionary. In this approach, we will first put all the elements of the column that needs to be inserted into a list. After that, we will add the list as a new column into the dataframe using the following syntax.
datframe_name[column_name]= element_lis
t
Here,
datframe_name
is the name of the dataframe into which the column has to be inserted.column_name
represents an string that contains the name of the new column.element_list
denotes list containing the elements that will be inserted to the dataframe.
Following is the python source code to insert a new column into a dataframe by indexing.
import pandas as pd
df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df['Height'] = column_data
print("The dataframe after inserting the column:")
print(df)
Output:
The dataframe before inserting the column:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
The dataframe after inserting the column:
Roll Name Language Height
0 1 Aditya Python 180
1 2 Sam Java 164
2 3 Chris C++ 170
Insert New Column Into a Dataframe Using the assign() Method
Instead of using indexing, we can use the assign()
method to add a new column into the dataframe. The assign()
method, when invoked on a dataframe, takes the column name and the list of elements of the new column as a keyword argument using the following syntax.
datframe_name.assign(column_name= element_list)
Here,
datframe_name
is the name of the dataframe into which the column has to be inserted.column_name
denotes the name of the new column.element_list
is the list containing the elements that will be inserted to the dataframe.
After execution, the assign()
method inserts the column into the dataframe and returns the updated dataframe as shown below.
import pandas as pd
df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df = df.assign(Height=column_data)
print("The dataframe after inserting the column:")
print(df)
Output:
The dataframe before inserting the column:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
The dataframe after inserting the column:
Roll Name Language Height
0 1 Aditya Python 180
1 2 Sam Java 164
2 3 Chris C++ 170
The above approaches are used to insert the new column at the end. We can also insert new columns at any position in the dataframe. For that, we can use the insert() method.
Insert New Column Into a Dataframe Using the insert() Method
With the insert()
method, we can insert a new column at any position in the dataframe. The insert()
method, when invoked on a dataframe, takes the position at which the new column will be inserted as its first input argument, the name of the new column as the second input argument, the list containing the elements of the new column as the third input argument. After execution, it inserts the column at the specified position in the dataframe. You can observe this in the following example.
import pandas as pd
df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df.insert(1, 'Height', column_data)
print("The dataframe after inserting the column:")
print(df)
Output:
The dataframe before inserting the column:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
The dataframe after inserting the column:
Roll Height Name Language
0 1 180 Aditya Python
1 2 164 Sam Java
2 3 170 Chris C++
Conclusion
In this article, we have discussed three approaches to insert a new column into a dataframe in python. To know more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
The post Insert New Column Into a Dataframe in Python appeared first on PythonForBeginners.com.