In this tutorial we will learn 3 methods to create new columns in Python.
Dataset:
In this tutorial we will make use of following CSV file:
Let us read our file using pandas' read_csv function. Do specify the file path where your file is located:
import pandas as pd
mydata = pd.read_csv("C:\\Users\\Employee_info.csv")
Let us create a copy of our dataset as mydata_copy and rename our columns.
mydata_copy= mydata.copy()
mydata_copy.columns = ['Name','Gender','Dept','Salary','Rating']
Let us create new column Monthly_Salary = Salary/12
Using square brackets [ ]:
To create a new colujmn we need to define new column name in [ ] square brackets. and define our formula as follows:
mydata_copy['Monthly_Salary'] = mydata_copy['Salary']/12
Note: We cannot create new columns by defining data_name.new_column_name. We MUST use [ ] square brackets to create new columns.
Following code will lead to an error.
mydata_copy.Monthly_Salary_exp= mydata_copy['Salary']/12
Using eval function:
In the eval function we can specify our column names directly without mentioning the name of our data repeatedly.
In the LHS, we have used eval( ) function i.e. we will not need to write mydata_copy['column name'] .
mydata_copy['Monthly_Salary2'] = mydata_copy.eval('Salary/12')
Using assign function:
If we have to create multiple columns then we can use assign function as follows:
Syntax:
new_data= old_data.assign( new_col_1= calculation_for_new_col1, new_col_2 = calculation_for_new_col2)
mydata_copy2 = mydata_copy.assign(Monthly_Salary3 = mydata_copy.Salary/12)
mydata_copy2.head(3)
Comments