How to create a new column in Pandas – Python
Let us learn to create a new column in pandas!
Pandas is an open-source data manipulation module. It is built on top of the Numpy Library. Pandas has a lot of benefits- Analysis becomes easy because of the flexibility of Python. It also is efficient when working with big data so it is a great Python module for data science. There are two important data types we use in pandas- Series and DataFrames. I would recommend you to read the documentation before you get started. Here is the link.
But first, let’s create a DataFrame using a dictionary. You can also add a new column to any of your already existing DataFrame.
import pandas as pd
Timetable={ 'Day':['Monday','Tuesday','Wednesday'],
'Class':['Statistics','Economics','Finance'],
'Faculty':['Rahul','Rohit','Ram']
}
df=pd.DataFrame(Timetable)
print(df)
Day |
Class |
Faculty |
|
|---|---|---|---|
0 |
Monday |
Statistics |
Rahul |
1 |
Tuesday |
Economics |
Rohit |
2 |
Wednesday |
Finance |
Ram |
There are various methods to add a new column in Pandas. One approach is by accessing the DataFrame and then calling the new column.
class_hour=['9am','8am','2pm'] df['class_hour']=class_hour print(df)
Day |
Class |
Faculty |
class_hour |
|
|---|---|---|---|---|
0 |
Monday |
Statistics |
Rahul |
9am |
1 |
Tuesday |
Economics |
Rohit |
8am |
2 |
Wednesday |
Finance |
Ram |
2pm |
Another method is using the insert() function:
Syntax for the insert function is -> DataFrame.insert(loc, column, value, allow_duplicates=False)
choose the location where you would want your new column to be added. Give a column name of your choice followed by the values of the new column to be added.
df.insert(loc=3,column='no_of_hours',value=[2,1,3]) print(df)
Day |
Class |
Faculty |
no_of_hours |
class_hour |
|
|---|---|---|---|---|---|
0 |
Monday |
Statistics |
Rahul |
2 |
9am |
1 |
Tuesday |
Economics |
Rohit |
1 |
8am |
2 |
Wednesday |
Finance |
Ram |
3 |
2pm |
There is another function called assign where we can assign values to our new column
df=df.assign(no_of_students=[35,23,50]) print(df)

These are some of the ways you can create a new column in pandas to the already existing DataFrame.
Leave a Reply