• Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • Labs The future of collective knowledge sharing
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Pandas: Assigning multiple *new* columns simultaneously

I have a DataFrame df with a column containing labels for each row (in addition to some relevant data for each row). I have a dictionary labeldict with keys equal to the possible labels and values equal to 2-tuples of information related to that label. I'd like to tack two new columns onto my frame, one for each part of the 2-tuple corresponding to the label for each row.

Here is the setup:

I can get what I want by running:

But how can I do this if I don't want to manually type out the two columns on the left side of the assignment? I.e. how can I create multiple new columns on the fly. For example, if I had 10-tuples in labeldict instead of 2-tuples, this would be a real pain as currently written. Here are a couple things that don't work:

This does work, but seems like a hack:

Better solutions?

smci's user avatar

  • 2 FYI your last method will work in 0.13 (without initially creating the column) –  Jeff Dec 29, 2013 at 21:55
  • Thanks Jeff. I have a similar question inspired by your comment here. I am having a similar problem to the one stated here –  Amelio Vazquez-Reina Sep 17, 2014 at 16:58
  • Seems like the answer is pythonically simple: stackoverflow.com/a/34074894/3494126 –  Ufos Oct 8, 2019 at 9:57

6 Answers 6

Just use result_type='expand' in pandas apply

and here some copy paste code

Markus Dutschke's user avatar

  • Use of apply was what I wanted but although this answer was helpful it made the assignment and function interdependent based on the order of the columns used as input and output. I ended up using the approach from @EricNess instead to capture this mapping entirely within the helper function. –  deaks Feb 12, 2020 at 23:24
  • Better use this anser: stackoverflow.com/a/76098907/7128154 –  Markus Dutschke Apr 25 at 8:08

You can use merge instead:

alko's user avatar

  • 4 you can get away without the reset_index using merge's left_index and right_on. –  Andy Hayden Dec 30, 2013 at 0:50
  • Note that merge generates a new dataframe instead of adding columns to an existing one. Also if there is no index column in the dataframe of your problem, you can't use merge. –  Louis Yang Dec 19, 2020 at 9:18

Instead of doing what you're doing with labeldict, you could make that information into a DataFrame and then join it with your original one:

BrenBarn's user avatar

If you want to add multiple columns to a DataFrame as part of a method chain, you can use apply . The first step is to create a function that will transform a row represented as a Series into the form you want. Then you can call apply to use this function on each row.

Eric Ness's user avatar

This should work:

AfterFray's user avatar

pandas apply method support this out of the box

A few years after writing my first answer to this question I got a bit better overview of the mechanics in pandas.

The func argument in apply takes a function: pd.Series -> Any whereas from the type of Any the exact processing of the returned value into the DataFrame is deduced.

Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function.

If you pass a lambda function: pd.Series -> pd.Series , you get exactly the required behavior.

apply can also be applied on pd.groupby outputs, achieving a more flexible alternative to .aggregate . You can find an example in the question: Python pandas groupby aggregate on multiple columns, then pivot

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .

Not the answer you're looking for? Browse other questions tagged python pandas or ask your own question .

  • The Overflow Blog
  • Fighting comment spam at Facebook scale (Ep. 602)
  • What it’s like being a professional workplace bestie (Ep. 603)
  • Featured on Meta
  • Moderation strike: Results of negotiations
  • Our Design Vision for Stack Overflow and the Stack Exchange network
  • Temporary policy: Generative AI (e.g., ChatGPT) is banned
  • Discussions experiment launching on NLP Collective
  • Call for volunteer reviewers for an updated search experience: OverflowAI Search

Hot Network Questions

  • Which airline is liable for compensation in case of missed connection?
  • Is there any way to find the distance covered by a train in a particular rail route between two stations?
  • What do Americans say instead of “can’t be bothered”?
  • What determines an electret microphone voltage?
  • Is there a (proposed) name for Coatlicue's progenitor?
  • how early can people build a giant clock?
  • Imbler v. Pachtman and Texas prosecutor Ken Anderson
  • Are high-starch potatoes hard (low-starch soft)?
  • Why did 1990s-2000s LCD all use 60 Hz refresh?
  • Can a public domain character be subject to trademarks?
  • Is it an Element?
  • Do interspecies couples exist in Zootopia?
  • Migrating Windows to a new internal drive, changing drive letters?
  • Can't uninstall Scala
  • move the instance collection to the point of contact with the plane?
  • Pros and cons to round PCB design
  • PNG files in photography workflow
  • Calculate the Distance to a Line Segment
  • Super slow deployments when using Solana CLI 16.x
  • Is it safe to create a public ID by hashing a private key?
  • Extend ethernet cable
  • Linux CLI tool for testing audio
  • sed: deleting the last line (of the input) stops my script
  • Would it make sense to use the "plus" and "minus" components of \baselineskip?

python assign multiple columns

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

  • Write For US

Spark By {Examples}

Pandas Add Multiple Columns to DataFrame

  • Post author: Malli
  • Post category: Pandas / Python
  • Post last modified: March 6, 2023

In pandas you can add/append multiple columns to the existing DataFrame using assign() function, this function updates the existing DataFrame with new multiple columns. DataFrame.insert() is also used to insert multiple columns however, this function returns a new Dataframe after adding columns. In this article, I will explain several ways how to add multiple columns to a DataFrame with examples.

Please enable JavaScript

1. Quick Examples of Add Multiple Columns DataFrame

If you are in a hurry, below are some quick examples of how to add multiple columns to DataFrame.

Now, Let’s  create Pandas DataFrame  using data from a  Python dictionary , where the columns are  Courses ,  Fee ,  Duration  and  Discount .

Yields below output.

2. Pandas Add Multiple Columns to a DataFrame Using df[]

Using df[] notation we can add multiple columns to Pandas DataFrame. This is the best example when we want to add a single column or multiple columns to DataFrame. Take two columns "Tutors” , and "Percent" as a list and pass them into df[] notation which will add these columns to the given DataFrame.

3. Add Multiple Columns Using Dataframe.assign()

DataFrame.assign() is also used to add/append multiple columns to the Pandas DataFrame, this function returns a new DataFrame after adding multiple columns to the existing DataFrame. Now let’s add multiple columns "Tutors” , and "Percent" to the DataFrame. Using assign() we cannot modify the existing DataFrame in place instead it returns a new DataFrame after adding multiple columns.

Yields the same output as above.

4. Pandas Add Multiple Columns Using insert()

DataFrame.insert() function is another function to add multiple columns to Pandas DataFrame at any position. Using this you can specify the index where you would like to add multiple columns. The below example adds multiple columns at the first position (Index 0) and fifth position (Index 4).

Note that in pandas, the Index starts from zero. insert() function updates the existing DataFrame object with the new multiple columns.

5. Complete Example of Adding Multiple Columns

6. conclusion.

In this article, I have explained how to add/append multiple columns to the existing Pandas DataFrame by using df[] , DataFrame.assing(), and DataFrame.insert() e.t.c. Also learned insert() is used to add multiple columns at any position of the DataFrame.

Happy Learning !!

Related Articles

  • Pandas Add Column based on Another Column
  • Pandas Add Column Names to DataFrame
  • Pandas Add Column to DataFrame
  • Add Column Name to Pandas Series
  • Pandas Add Constant Column to DataFrame
  • Pandas – Add an Empty Column to a DataFrame
  • Convert Pandas Column to Lowercase
  • How to Convert Pandas Uppercase Column
  • Pandas Filter DataFrame by Multiple Conditions
  • Select pandas columns based on condition
  • https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.insert.html

You may also like reading:

  • Pandas groupby() and sum() With Examples
  • Install Pandas on Windows Step-by-Step
  • Pandas Change String to Date in DataFrame
  • Pandas Merge Two DataFrames
  • pandas DataFrame filter() – Usage & Examples
  • How to read CSV without headers in pandas
  • How to Create Pandas Pivot Multiple Columns
  • How to Plot the Boxplot from DataFrame?
  • pandas Convert Datetime to Seconds

Leave a Reply Cancel reply

Save my name, email, and website in this browser for the next time I comment.

You are currently viewing Pandas Add Multiple Columns to DataFrame

  • Data Structure In Pandas
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • Pandas Exercise
  • Data Analysis
  • Machine Learning Math
  • Machin Learning
  • Deep Learning
  • Deep Learning Projects
  • Computer vision
  • Data science
  • Deep learning interview question
  • Machin Learning Interview question
  • Write an Interview Experience
  • Share Your Campus Experience
  • Methods to Round Values in Pandas DataFrame
  • How to Convert Float to Datetime in Pandas DataFrame?
  • Highlight the negative values red and positive values black in Pandas Dataframe
  • Display the Pandas DataFrame in table style
  • How to Convert Integers to Floats in Pandas DataFrame?
  • Split large Pandas Dataframe into list of smaller Dataframes
  • Count Values in Pandas Dataframe
  • Get Seconds from timestamp in Python-Pandas
  • Exporting Pandas DataFrame to JSON File
  • How to convert a dictionary to a Pandas series?
  • Display the Pandas DataFrame in table style and border around the table and not around the rows
  • Convert Floats to Integers in a Pandas DataFrame
  • Find Exponential of a column in Pandas-Python
  • Replace Negative Number by Zeros in Pandas DataFrame
  • Convert a series of date strings to a time series in Pandas Dataframe
  • How to Count Distinct Values of a Pandas Dataframe Column?
  • Split Pandas Dataframe by Rows
  • How to Create a Pivot table with multiple indexes from an excel sheet using Pandas in Python?
  • Select all columns, except one given column in a Pandas DataFrame
  • Make a gradient color mapping on a specified column in Pandas
  • Adding new column to existing DataFrame in Pandas
  • Python map() function
  • Read JSON file using Python
  • How to get column names in Pandas dataframe
  • Taking input in Python
  • Read a file line by line in Python
  • Python Dictionary
  • Iterate over a list in Python
  • Enumerate() in Python
  • Reading and Writing to text files in Python

Add multiple columns to dataframe in Pandas

In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. 

Add multiple columns to a DataFrame using Lists

Add DataFrame columns using Lists

Add multiple columns to a data frame using Dataframe.assign() method

Using DataFrame.assign() method, we can set column names as parameters and pass values as list to replace/create the columns.

Added multiple columns using DataFrame assign() Method

Add multiple columns to a data frame using Dataframe.insert() method

Using DataFrame.insert() method, we can add new columns at specific position of the column name sequence. Although insert takes single column name, value as input, but we can use it repeatedly to add multiple columns to the DataFrame. 

Added multiple columns using DataFrame insert() Method

Add multiple columns to a data frame using Dictionary and zip()

Using Dict and zip() we can create a mapping of key values, which can be assigned to a new column name.

Added multiple columns using Dictionary and zip()

Please Login to comment...

Improve your coding skills with practice.

  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

PythonForBeginners.com

PythonForBeginners.com

Learn By Example

Pandas Assign New Columns to a DataFrame

Author: Aditya Raj Last Updated: March 8, 2023

Pandas dataframes are the data structures that we use to handle tabular data in python. This article discusses different ways to assign new columns to pandas dataframe using the assign() method.

The Pandas assign() Method

Pandas assign a column to a dataframe, assign a column based on another column, assign multiple columns to a dataframe.

The assign() method is used to assign new columns to a pandas dataframe. It has the following syntax.

In the above function, the column names and the values for the columns are passed as keyword arguments. Here, column names are the keywords, and list or series objects containing the data for the column are the corresponding values.

python assign multiple columns

When we invoke the assign() method on a dataframe df, it takes column names and series objects as its input argument. After execution, the assign() method adds the specified column to the dataframe df and returns the modified dataframe. 

To assign a new column to the pandas dataframe using the assign() method, we will pass the column name as its input argument and the values in the column as the value assigned to the column name parameter.

After execution, the assign() method returns the modified dataframe. You can observe this in the following example.

In the above example, we created a column "Name" in the input dataframe using a list and the a ssign() method.

Instead of a list, we can also assign a pandas series to the column name parameter to create a new column in the dataframe as shown below.

In this example, we passed a series to the assign() method as an input argument instead of a list. However, you can observe that the output dataframe in this example is the same as the previous example.

We can also create a column based on another column in a pandas dataframe. For this, we will first create a series based on another column. Then, we can use the assign() method and pass the column name with the series as its input to assign the column to the pandas dataframe.

You can observe this in the following example.

In this example, we have created the GPI column using the "Maths" column. For this, we created a series by dividing the Maths column by 10. Then, we assigned the new series to the GPI keyword as an input argument in the assign() method. You can also use the pandas apply method to create a new series in this case.

To assign multiple columns to the pandas dataframe, you can use the assign() method as shown below.

In this example, we assigned two columns to the pandas dataframe using the assign() method. For this, we passed both the column names and their values as keyword arguments to the assign() method.

In this article, we have discussed different ways to assign columns to a pandas dataframe. To learn more about python programming, you can read this article on how to use the insert() method to insert a column into a dataframe . You might also like this article on how to convert epoch to datetime in python .

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

More Python Topics

72317/how-add-multiple-columns-to-pandas-dataframe-one-assignment

  • How to add multiple columns to pandas dataframe...

How to add multiple columns to pandas dataframe in one assignment

I'm new to pandas and trying to figure out how to add multiple columns to pandas simultaneously. Any help here is appreciated. Ideally I would like to do this in one step rather than multiple repeated steps.

  • python-programming

python assign multiple columns

Your comment on this question:

1 answer to this question., your answer.

Hello @kartik,

You could use assign with a dict of column names and values.

Hope it helps!!

python assign multiple columns

  • ask related question

Your comment on this answer:

Related questions in python, how to convert multiple columns to string in pandas dataframe.

Hi, To convert multiple columns to string, include a list of ... READ MORE

How to change the order of DataFrame columns in pandas?

Hi@akhtar, You can rearrange a DataFrame object by ... READ MORE

  • pandas-series

How to combine two columns of text in pandas dataframe?

If both columns are strings, you can ... READ MORE

  • pandas-dataframe
  • python-dataframe

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ... READ MORE

how can i randomly select items from a list?

You can also use the random library's ... READ MORE

  • python-list
  • python-datatypes
  • python-functions
  • python-sequence-types
  • python-types

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ... READ MORE

how do i use the enumerate function inside a list?

Enumerate() method adds a counter to an ... READ MORE

Lowercase in Python

You can simply the built-in function in ... READ MORE

  • python-file
  • python-string
  • python-test-processing
  • python-services
  • python-lowercase

How to change/update cell value in Python Pandas dataframe?

You can use the at() method to ... READ MORE

How to check if any value is NaN in a Pandas DataFrame?

Hello @kartik, If you need to know how ... READ MORE

  • All categories

python assign multiple columns

Join the world's most active Tech Community!

Welcome back to the world's most active tech community.

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

Subscribe to our Newsletter, and get personalized recommendations.

Google

Already have an account? Sign in .

python assign multiple columns

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using loc with assignment and multiple columns fails #16187

@cancan101

cancan101 commented May 1, 2017

  • 👍 4 reactions

@jreback

jreback commented May 1, 2017

Sorry, something went wrong.

@jreback

rchurt commented Jul 12, 2020

  • ❤️ 7 reactions

No branches or pull requests

@cancan101

python assign multiple columns

Select Multiple Columns of Pandas DataFrame in Python (4 Examples)

In this Python article you’ll learn how to extract certain columns of a pandas DataFrame .

The article will consist of four examples for the selection of DataFrame variables. To be more precise, the article is structured as follows:

Here’s how to do it:

pandas Library Creation of Example Data

As the first step, we have to import the pandas library to Python:

import pandas as pd # Load pandas

Next, we can create an example pandas DataFrame by running the following Python syntax:

As you can see based on the previous output, we have created a pandas DataFrame with five rows and five variables called x1, x2, x3, x4, and x5.

In the following examples, I’ll explain how to select some of these variables and how to store them in a new data set.

Keep on reading!

Example 1: Extract DataFrame Columns Using Column Names & Square Brackets

This example shows how to use the names of our variables and square brackets to subset our pandas DataFrame.

Have a look at the following Python code:

As you can see, we have created a new pandas DataFrame called data_new1 that contains only the variables x1, x3, and x5. The columns x2 and x4 have been dropped.

Looks good!

However, the Python programming language provides many alternative ways on how to select and remove DataFrame columns. In the following examples I’ll show some of these alternatives!

Example 2: Extract DataFrame Columns Using Column Names & DataFrame Function

In this example, I’ll illustrate how to use the column names and the DataFrame() function of the pandas library to get a new DataFrame with specific variables.

Check out the following syntax and its output:

We have created another pandas DataFrame called data_new2, which contains exactly the same variables and values as the DataFrame that we have created in Example 1. However, this time we have used the DataFrame() function.

Example 3: Extract DataFrame Columns Using Indices & iloc Attribute

So far, we have subsetted our DataFrame using the names of our columns. However, it is also possible to use the column indices to select certain variables from a DataFrame.

The following Python syntax demonstrates how to use the iloc command in combination with the column index to retain only some variables of our input DataFrame:

Again, we have created the same output as in the previous examples.

Example 4: Extract DataFrame Columns Using Indices & columns Attribute

In Example 4, I’ll illustrate another alternative on how to use column indices to keep only particular columns.

More precisely, we are using the columns argument to retain certain variables:

Even though we have used a different code, the output is again the same as in the previous examples. So as you have seen, we have many alternatives when we want to remove unnecessary variables from a data matrix.

Video & Further Resources on the Topic

Any questions left? I have recently released a video on my YouTube channel , which shows the Python syntax of this article. You can find the video below:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

accept

Have a look at the following video that was published by Corey Schafer on his YouTube channel. He’s illustrating some examples on how to select rows and columns of a pandas DataFrame in the video.

In addition to the video, you may want to read some of the related articles of my website:

  • Select Rows of pandas DataFrame by Index in Python
  • Extract Top & Bottom N Rows from pandas DataFrame
  • pandas DataFrames Operations in Python
  • Modify & Edit pandas DataFrames in Python
  • Python Programming Overview

In this Python tutorial you have learned how to subset a DataFrame . In case you have any further questions, let me know in the comments.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Post Comment

Joachim Schork Statistician Programmer

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

Related Tutorials

Convert timedelta to Seconds in Python (Example)

Convert timedelta to Seconds in Python (Example)

mode() & multimode() Functions of statistics Module in Python (2 Examples)

mode() & multimode() Functions of statistics Module in Python (2 Examples)

IMAGES

  1. python

    python assign multiple columns

  2. python

    python assign multiple columns

  3. [Python] DataFrame is split into multiple columns and one row is split

    python assign multiple columns

  4. sorting by value/multiple column using Python panda in dataFrame

    python assign multiple columns

  5. pandas

    python assign multiple columns

  6. Python DataFrame

    python assign multiple columns

VIDEO

  1. Python Tutorial Part 22: Add to a Set

  2. How to return multiple values from function #python #coding

  3. Python

  4. Python program to insert values to a table from user input

  5. Function returns MULTIPLE values in PYTHON 🐍#shorts #python #programming

  6. SORTING

COMMENTS

  1. The Ultimate Guide to Choosing the Right Python Developer Online Course

    Are you looking to become a Python developer? With its versatility and widespread use in the tech industry, Python has become one of the most popular programming languages today. One factor to consider is whether you prefer self-paced learn...

  2. What Element Has Six Valence Electrons?

    There are multiple elements that have six valence electrons, including oxygen and sulfur. These elements can be found in the sixteenth group in the vertical column of the periodic table, also known as the chalcogens.

  3. What Is a Data Series in Excel?

    A data series in Excel is a collection of rows or columns that are displayed in a chart. A person can add multiple charts to a data series. Individuals can represent their data in various ways depending on the type of graph they want to use...

  4. Pandas: Assigning multiple *new* columns simultaneously

    If you want to add multiple columns to a DataFrame as part of a method chain, you can use apply . The first step is to create a function that

  5. Pandas Add Multiple Columns to DataFrame

    In pandas you can add/append multiple columns to the existing DataFrame using assign() function, this function updates the existing

  6. pandas.DataFrame.assign

    Assigning multiple columns within the same assign is possible. Later items in '**kwargs' may refer to newly created or modified columns in 'df';

  7. Add multiple columns to dataframe in Pandas

    Using DataFrame.assign() method, we can set column names as parameters and pass values as list to replace/create the columns. Python3

  8. Python

    Here, we are going to learn how to assign multiple new columns simultaneously in Python pandas?

  9. Pandas Assign New Columns to a DataFrame

    To assign multiple columns to the pandas dataframe, you can use the assign() method as shown below. import pandas as pd myDicts=[{"Roll":1,"

  10. How to add multiple columns to pandas dataframe in one assignment

    You could use assign with a dict of column names and values. In [1069]: df.assign(**{'col_new_1': np.nan, 'col2_new_2': 'dogs', 'col3_new_3': 3})

  11. Pandas: Assigning multiple *new* columns simultaneously

    What you can do is to use merge instead: >>> ld = pd.DataFrame(labeldict).T. >>> ld.columns = ['color', 'size']. >>> ld.index.name = 'label'.

  12. Assignment to multiple columns only works if they existed before

    Assignment to multiple columns of a :class:`DataFrame` when some of the ... constructed with the right values. .. ipython:: python df = pd.

  13. Using loc with assignment and multiple columns fails #16187

    Code Sample, a copy-pastable example if possible # Your code here df = pd.DataFrame({'a': range(4), 'b': range(4,8)}) df.loc[df.a==2, ['a'

  14. Select Multiple Columns of Pandas DataFrame in Python (4 Examples)

    How to subset particular variables of a pandas DataFrame in Python - 4 Python programming examples - Actionable Python syntax.