| by Arround The Web | No comments

Append a Column to the Pandas DataFrame

There are several ways to append the column/s to the DataFrame in Pandas. It is also possible to add the constant, specific, and missing values. We can also utilize the values from the existing column and specify these values in the new column. In this guide, we will use the pandas.DataFrame.assign, pandas.DataFrame.insert and pandas.DataFrame.map functions to append new columns to the existing DataFrame. Also, we will see how to add the columns using the square brackets [].

Using Pandas.DataFrame.Assign

The pandas.DataFrame.assign() function assigns a new column to the Pandas DataFrame. We can add multiple columns at a time using this function. It returns the DataFrame with the added column/s along with the existing columns.

Syntax:

Let’s see the syntax and the parameter that are passed to this function:

pandas.DataFrame.assign(**kwargs)

It takes the column name/s and values that are passed to the column as a parameter. We need to assign the column names through a list to the new column name.

Example 1: Append a Single Column

Create the “Contacts” DataFrame with one column which is “Name”. Append the “Title” column to this DataFrame with [“Manager”, “Analyst”, “CEO”, “Trainee”] as values.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append single column - Title
Contacts1=Contacts.assign(Title=['Manager','Analyst','CEO','Trainee'])
print(Contacts1,"\n")

Output:

The “Title” column is appended to the DataFrame.

Example 2: Append Multiple Columns

Use the previous DataFrame and append three columns – “Age”, “gender”, and “Salary” – to it.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append three columns - Age, gender & Salary
Contacts2=Contacts.assign(Age=[34,23,56,21],gender=['M','F','M','F'],Salary=[45000,23000,100000,20000])
print(Contacts2,"\n")

Output:

Three columns are appended to the “Contacts” DataFrame – Age=[34,23,56,21], gender=[‘M’,’F’,’M’,’F’], Salary=[45000,23000,100000,20000].

Using Pandas.DataFrame.Insert

The pandas.DataFrame.insert() function inserts the column at the specific position. In our scenario, we need to insert at the last position.

Syntax:

Let’s see the syntax and the parameter that are passed to this function:

pandas.DataFrame.insert(loc, column, value, allow_duplicates)

  1. The first parameter takes the column index position. The index starts from 0. Set this parameter to len(DataFrame.columns). This returns the last column index position minus 1.
  2. The column is the label for the new column name.
  3. The value can be a list of values or a constant value.
  4. We can allow duplicate values by setting the “allow_duplicates” parameter to “True”.

Example 1: Append the Column with Constant Value

Create the “Contacts” DataFrame with one column which is “Name”. Append the “Salary” column to this DataFrame with the constant value of 1000.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append Salary column with constant value - 1000.
Contacts.insert(1,'Salary', 1000 )
print(Contacts,"\n")

Output:

The first output is the actual DataFrame and the “Salary” column is appended to the “Contacts” DataFrame with the constant value of 1000.

Example 2: Append the Column with Multiple Values

Append the same column to this DataFrame but pass different values to the column through a list.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append Salary column with different values
Contacts.insert(1,'Salary', [45000,23000,100000,20000] )
print(Contacts,"\n")

Output:

Now, each row holds different values in the “Salary” column.

Using Square Brackets []

We can access specific columns using the square brackets. If we assign values by specifying the column inside the square bracket, a new column is created at the end of the DataFrame.

Constant value:DataFrame[‘new_column’] = value
Specific values :DataFrame[‘new_column’] = [value1,value2,…]

Example 1: Append the Column with Constant Value

Create the “Contacts” DataFrame with one column which is “Name”. Append the “Age” column to this DataFrame with the constant value of 30.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append Age column
Contacts['Age']=30
print(Contacts)

Output:

The first output is the actual DataFrame and the “Age” column is appended to the “Contacts” DataFrame with the constant value of 30.

Example 2: Append the Column with Specific Values

Append the “Age” column by passing different values.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Append Age column with different values
Contacts['Age']=[30,24,34,67]
print(Contacts)

Output:

Now, the “Age” column holds different values.

Using Pandas.DataFrame.Map

The pandas.DataFrame.map() is used to apply a function to the DataFrame elementwise. In our scenario, we pass a dictionary with the key as values from the existing column and the value as the new values.

pandas.DataFrame.map({‘existing_value’:’new_values’,….)

Example:

Let’s append the “Title” column to the DataFrame by mapping the values to the existing column which is “Name” {“Sravan”:”Manager”,”Manju”:”Trainee”,”Shiva”:”Analyst”,”Gowthami”:”Director”}.

import pandas

# Create pandas DataFrame with one column
Contacts = pandas.DataFrame([['Sravan'],
                             ['Manju'],
                             ['Shiva'],
                             ['Gowthami']], columns=['Name'])
print(Contacts,"\n")

# Add the Title column using the map() function based on elements in the Name column.
Contacts['Title'] = Contacts['Name'].map({"Sravan":"Manager", "Manju":"Trainee", "Shiva":"Analyst","Gowthami":"Director"})
print(Contacts,"\n")

Output:

Based on the existing data in the “Name” column, the values are mapped in the “Title” column.

Conclusion

Now, you are able to append the column to the existing Pandas DataFrame by any of the four functions that are discussed in this tutorial. In each scenario, we discussed how to add the constant value and specific values to the created column. Only one DataFrame is used in all the examples to understand the examples better. For deeper tutorials, the following links are related to the functions that are used in this guide –Pandas insert(), Pandas assign(), Pandas map().

Share Button

Source: linuxhint.com

Leave a Reply