Adding new column to existing DataFrame in Pandas - GeeksforGeeks

06 Nov, 2021
Let’s discuss how to add new columns to the existing DataFrame in Pandas. There are multiple ways we can do this task.
Method #1: By declaring a new list as a column. 
Python3
# Import pandas packageimport pandas as pd # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],        'Height': [5.1, 6.2, 5.1, 5.2],        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} # Convert the dictionary into DataFramedf = pd.DataFrame(data) # Declare a list that is to be converted into a columnaddress = ['Delhi', 'Bangalore', 'Chennai', 'Patna'] # Using 'Address' as the column name# and equating it to the listdf['Address'] = address # Observe the resultdf
Output: 
Note that the length of your list should match the length of the index column otherwise it will show an error. 
YouTubeGeeksforGeeks502K subscribersHow to Add New Column in Pandas Dataframe? | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 10:35•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=IZ8oCcVB3iA" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Method #2: By using DataFrame.insert()It gives the freedom to add a column at any position we like and not just at the end. It also provides different options for inserting the column values.
Python3
# Import pandas packageimport pandas as pd # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],        'Height': [5.1, 6.2, 5.1, 5.2],        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} # Convert the dictionary into DataFramedf = pd.DataFrame(data) # Using DataFrame.insert() to add a columndf.insert(2, "Age", [21, 23, 24, 21], True) # Observe the resultdf
Output: 
Method #3: Using Dataframe.assign() methodThis method will create a new dataframe with a new column added to the old dataframe.
Python3
# Import pandas packageimport pandas as pd  # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],        'Height': [5.1, 6.2, 5.1, 5.2],        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}    # Convert the dictionary into DataFramedf = pd.DataFrame(data) # Using 'Address' as the column name and equating it to the listdf2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'])  # Observe the resultdf2
Output: 
Method #4: By using a dictionaryWe can use a Python dictionary to add a new column in pandas DataFrame. Use an existing column as the key values and their respective values will be the values for a new column. 
Python3
# Import pandas packageimport pandas as pd # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],        'Height': [5.1, 6.2, 5.1, 5.2],        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} # Define a dictionary with key values of# an existing column and their respective# value pairs as the # values for our new column.address = {'Delhi': 'Jai', 'Bangalore': 'Princi',           'Patna': 'Gaurav', 'Chennai': 'Anuj'} # Convert the dictionary into DataFramedf = pd.DataFrame(data) # Provide 'Address' as the column namedf['Address'] = address # Observe the outputdf
Output: 
 
reenadevi98412200
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Create a Pandas DataFrame from Lists
*args and **kwargs in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
sum() function in Python
Print lists in Python (4 Different Ways)
Convert integer to string in Python