Adding new column to existing DataFrame in Pandas

Pandas Data Frame is a two-layered information structure, i.e., information is adjusted in an even manner in lines and sections. It tends to be made utilizing python dict, rundown and series and so forth In this article we will perceive how to add another section to a current information outline. So first we should make an information outline utilizing pandas series. In the underneath model we are changing a pandas series over to a Data Frame of one section, giving it a segment name Month_no.

In pandas you can add another segment to the current DataFrame utilizing DataFrame.insert() technique, this strategy refreshes the current DataFrame with another section. DataFrame.assign() is likewise used to embed another segment in any case, this technique returns another Dataframe subsequent to adding another section.

In this article I will cover instances of how to add different sections, adding a steady worth, getting new segments from a current column,s and enhancing the Pandas DataFrame

Python, a deciphered, broadly useful, significant level programming language, has as of late turned into an incredible figuring language because of its immense assortment of libraries and simple to execute nature. The prominence of Python took a tremendous jump with the execution of information science and information examination. There are great many libraries that can be incorporated with Python to make it work on any vertical productively.

Pandas is one such information investigation library planned unequivocally for Python to perform information control and information examination. The Pandas library comprises of explicit information constructions and tasks to manage mathematical tables, breaking down information, and work with time series. In this article, you will get to know how to add segments to DataFrame in Pandas that as of now exists.

What is DataFrame?

Prior to being familiar with how to add another section to the current DataFrame, let us first take a brief look at DataFrames in Quite a while. DataFrame is an impermanent information structure as a two-layered cluster that can store heterogeneous qualities with marked tomahawks (lines and segments). DataFrame is an information structure where the information remains put away in a sensible plan of plain (meeting lines and segments) style. The three significant parts of a DataFrame are lines, segments, and information. Making a DataFrame in Python is extremely simple.

1- By declaring a new list as a column. 

# 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)
# Declare a list that is to be converted into a column
address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
# Observe the result
df

Output:

DataFrame

2- By using DataFrame.insert()

# 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
df

Output:

3- Using Dataframe.assign() method

# 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 'Address' as the column name and equating it to the list
df2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'])
 
# Observe the result
df2

Output:

DataFrame

4- By using a dictionary

# 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']}
# 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 DataFrame
df = pd.DataFrame(data)
# Provide 'Address' as the column name
df['Address'] = address
# Observe the output
df

Output:

DataFrame

Also ReadWhat are the most widely used gadgets in the world?

Leave a Reply

Your email address will not be published.