Skip to main content
TopMiniSite

Back to all posts

How to Convert Multiple Set Of Column to Single Column In Pandas?

Published on
3 min read
How to Convert Multiple Set Of Column to Single Column In Pandas? image

Best Data Transformation Tools to Buy in October 2025

1 Data Engineering with AWS: Acquire the skills to design and build AWS-based data transformation pipelines like a pro

Data Engineering with AWS: Acquire the skills to design and build AWS-based data transformation pipelines like a pro

BUY & SAVE
$23.44 $51.99
Save 55%
Data Engineering with AWS: Acquire the skills to design and build AWS-based data transformation pipelines like a pro
2 Augmented Analytics: Enabling Analytics Transformation for Data-Informed Decisions

Augmented Analytics: Enabling Analytics Transformation for Data-Informed Decisions

BUY & SAVE
$4.01 $59.99
Save 93%
Augmented Analytics: Enabling Analytics Transformation for Data-Informed Decisions
3 Data Engineering with dbt: A practical guide to building a cloud-based, pragmatic, and dependable data platform with SQL

Data Engineering with dbt: A practical guide to building a cloud-based, pragmatic, and dependable data platform with SQL

BUY & SAVE
$30.13 $49.99
Save 40%
Data Engineering with dbt: A practical guide to building a cloud-based, pragmatic, and dependable data platform with SQL
4 Data Driven Health: How I Hacked My Health, Fixed Chronic Illnesses, and Survived Emergency Surgery with DIY Open Source Tools

Data Driven Health: How I Hacked My Health, Fixed Chronic Illnesses, and Survived Emergency Surgery with DIY Open Source Tools

BUY & SAVE
$9.99
Data Driven Health: How I Hacked My Health, Fixed Chronic Illnesses, and Survived Emergency Surgery with DIY Open Source Tools
5 Data Mesh: Delivering Data-Driven Value at Scale

Data Mesh: Delivering Data-Driven Value at Scale

BUY & SAVE
$43.99 $79.99
Save 45%
Data Mesh: Delivering Data-Driven Value at Scale
6 Learning Power Query: Simplify data cleaning and analysis with Excel’s most powerful tool (English Edition)

Learning Power Query: Simplify data cleaning and analysis with Excel’s most powerful tool (English Edition)

BUY & SAVE
$39.95
Learning Power Query: Simplify data cleaning and analysis with Excel’s most powerful tool (English Edition)
7 Modern Data Analytics in Excel: Using Power Query, Power Pivot, and More for Enhanced Data Analytics

Modern Data Analytics in Excel: Using Power Query, Power Pivot, and More for Enhanced Data Analytics

BUY & SAVE
$39.09 $59.99
Save 35%
Modern Data Analytics in Excel: Using Power Query, Power Pivot, and More for Enhanced Data Analytics
8 Leading Digital: Turning Technology into Business Transformation

Leading Digital: Turning Technology into Business Transformation

BUY & SAVE
$15.38 $32.00
Save 52%
Leading Digital: Turning Technology into Business Transformation
9 Digital Operations Transformation: Integrating ITOps, DevOps, DataOps, and MLOps for End-to-End Efficiency

Digital Operations Transformation: Integrating ITOps, DevOps, DataOps, and MLOps for End-to-End Efficiency

BUY & SAVE
$15.99
Digital Operations Transformation: Integrating ITOps, DevOps, DataOps, and MLOps for End-to-End Efficiency
+
ONE MORE?

To convert multiple sets of columns to a single column in pandas, you can use the melt() function. This function reshapes the DataFrame from wide format to long format by unpivoting the specified columns into rows. By specifying the id_vars parameter with the columns you want to remain as is, and value_vars parameter with the columns you want to convert to a single column, you can achieve this transformation easily.

How to stack columns into a single column in pandas?

You can stack columns into a single column in pandas by using the pd.melt() function. This function will combine multiple columns into a single column by "melting" or transforming the data.

Here is an example of how to stack columns into a single column in pandas:

import pandas as pd

Create a sample dataframe

df = pd.DataFrame({ 'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie'], 'Grade_Math': [90, 85, 95], 'Grade_Science': [88, 92, 87] })

Stack columns into a single column using pd.melt()

df_stacked = pd.melt(df, id_vars=['ID', 'Name'], var_name='Subject', value_name='Grade')

print(df_stacked)

This will result in a new dataframe df_stacked where the columns Grade_Math and Grade_Science have been stacked into a single column called Grade, with an additional column Subject to specify which original column the value came from.

How to concatenate columns in pandas?

To concatenate columns in pandas, you can use the + operator or the pd.concat() function.

Here's an example using the + operator:

import pandas as pd

Create a sample DataFrame

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

Concatenate columns A and B into a new column C

df['C'] = df['A'].astype(str) + df['B'].astype(str)

print(df)

Alternatively, you can use the pd.concat() function to concatenate columns along either the rows or columns axis. Here's an example:

import pandas as pd

Create a sample DataFrame

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

Concatenate columns A and B along the columns axis

df_concatenated = pd.concat([df['A'], df['B']], axis=1)

print(df_concatenated)

These are two ways you can concatenate columns in pandas.

What is the most effective way to combine multiple columns into a single column in pandas?

One of the most effective ways to combine multiple columns into a single column in pandas is by using the apply() function along with a lambda function. This allows you to apply a custom function to each row in the DataFrame and return a new column with the combined values.

For example, if you have columns 'A', 'B', and 'C' in your DataFrame and you want to combine them into a single column 'D', you can use the following code:

import pandas as pd

Create a sample DataFrame

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

Combine columns 'A', 'B', and 'C' into a single column 'D'

df['D'] = df.apply(lambda row: str(row['A']) + str(row['B']) + str(row['C']), axis=1)

Print the updated DataFrame

print(df)

This will create a new column 'D' in the DataFrame that contains the combined values from columns 'A', 'B', and 'C. You can modify the lambda function to suit your specific requirements for combining the columns.