Collapse multiple Columns in Pandas Last Updated : 08 Mar, 2019 Comments Improve Suggest changes 1 Likes Like Report While operating dataframes in Pandas, we might encounter a situation to collapse the columns. Let it be cumulated data of multiple columns or collapse based on some other requirement. Let's see how to collapse multiple columns in Pandas. Following steps are to be followed to collapse multiple columns in Pandas: Step #1: Load numpy and Pandas. Step #2: Create random data and use them to create a pandas dataframe. Step #3: Convert multiple lists into a single data frame, by creating a dictionary for each list with a name. Step #4: Then use Pandas dataframe into dict. A data frame with columns of data and column for names is ready. Step #5: Specify which columns are to be collapsed. That can be done by specifying the mapping as a dictionary, where the keys are the names of columns to be combined or collapsed and the values are the names of the resulting column. Example 1: Python3 # Python program to collapse # multiple Columns using Pandas import pandas as pd # sample data n = 3 Sample_1 = [57, 51, 6] Sample_2 = [92, 16, 19] Sample_3 = [15, 93, 71] Sample_4 = [28, 73, 31] sample_id = zip(["S"]*n, list(range(1, n + 1))) s_names = [''.join([w[0], str(w[1])]) for w in sample_id] d = {'s_names': s_names, 'Sample_1': Sample_1, 'Sample_2': Sample_2, 'Sample_3': Sample_3, 'Sample_4': Sample_4} df_1 = pd.DataFrame(d) mapping = {'Sample_1': 'Result_1', 'Sample_2': 'Result_1', 'Sample_3': 'Result_2', 'Sample_4': 'Result_2'} df = df_1.set_index('s_names').groupby(mapping, axis = 1).sum() df.reset_index(level = 0) Output: Example 2: Python3 # Python program to collapse # multiple Columns using Pandas import pandas as pd df = pd.DataFrame({'First': ['Manan ', 'Raghav ', 'Sunny '], 'Last': ['Goel', 'Sharma', 'Chawla'], 'Age':[12, 24, 56]}) mapping = {'First': 'Full Name', 'Last': 'Full Name'} df = df.set_index('Age').groupby(mapping, axis = 1).sum() df.reset_index(level = 0) Output: Create Quiz Comment A apeksharustagi1998 Follow 1 Improve A apeksharustagi1998 Follow 1 Improve Article Tags : Python Python-pandas Python pandas-dataFrame pandas-dataframe-program Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like