Data Science

Pandas – Append Columns to Dataframe

In this post, you will learn different techniques to append or add one column or multiple columns to Pandas Dataframe (Python). There are different scenarios where this could come very handy. For example, when there are two or more data frames created using different data sources, and you want to select a specific set of columns from different data frames to create one single data frame, the methods given below can be used to append or add one or more columns to create one single data frame. It will be good to know these methods as it helps in data preprocessing stage of building machine learning models.

In this post, we will work the following two Pandas data frames. The requirement is to append second data frame (df2) to first dataframe (df1)

import pandas as pd
#
# Dataframe 1 having 3 columns
#
df1 = pd.DataFrame([
    ['Ajitesh', 'M', 'Software'],
    ['Sumit', 'M', 'Software'],
    ['Sarojini', 'M', 'Business'],
    ['Amitabh', 'M', 'Digital Marketing']
])
df1.columns = ['name', 'gender', 'profession']
#
# Dataframe 2 having 2 columns
#
df2 = pd.DataFrame([
    ['tall', 84],
    ['tall', 88],
    ['short', 62],
    ['very tall', 85]
])
df2.columns = ['height', 'weight']

This is how the above data frames look like.

Fig 1. Dataframes

Technique 1: Use Join

Use Dataframe Join method to append one or more columns to existing data frame. Note that columns of df2 is appended to df1. The following code will work:

df1 = df1.join(df2)
Fig 2. Join method to append columns

Technique 2: Use Concat

Use Pandas concat method to append one or more columns to existing data frame. The way this is different from join method is that concat method (static method) is invoked on pandas class while join method is invoked on an instance of data frame. Note that columns of df2 is appended to df1. The following code will work:

df1 = pd.concat([df1, df2], axis=1)
Fig 3. Pandas concat method to append the columns to the dataframe

Conclusion

In this post, you learned about how to append or add one column or multiple columns to the Pandas data frame. Here are two commands which can be used:

  • Use Dataframe join command to append the columns
  • Use Pandas concat command to append the columns
  • Both methods can be used to join multiple columns from different data frames and create one data frame.
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

1 hour ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

16 hours ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

2 days ago

How Indexing Works in LLM-Based RAG Applications

When building a Retrieval-Augmented Generation (RAG) application powered by Large Language Models (LLMs), which combine…

6 days ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

6 days ago

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

4 weeks ago