Python 3 Pandas DataFrame.insert() Method Script to Add New Columns to Existing DataFrame Full Example Project For Beginners

Python 3 Pandas DataFrame.insert() Method Script to Add New Columns to Existing DataFrame Full Example Project For Beginners

 

 

Welcome folks today in this blog post we will be adding new columns to existing dataframe using insert method in python using pandas library. All the full source code of the application is shown below.

 

 

 

 

Get Started

 

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

# Import pandas package
import pandas as pd

# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)

# Using DataFrame.insert() to add a column
df.insert(2, "Age", [21, 23, 24, 21], True)

# Observe the result
print(df)

 

See also  Python 3 Tkinter ATM Bank Management System Using MySQL Database GUI Desktop App Full Project For Beginners

 

Now if you execute the python script by typing the below command as shown below

 

 

python app.py

 

 

 

Leave a Reply