Add together Column To Dataframe Pandas

One of the nigh common Pandas tasks you'll do is add more information to your DataFrame. This means you demand to become an expert at calculation a column to your DataFrame.

five ways to add a new column to your DataFrame in Pandas:

  • By declaring a new column proper noun with a scalar or list of values
  • By using df.insert()
  • Using df.assign()
  • Using a dictionary
  • Using .loc[]

Pseudo code: Using a new scalar or list of data, add a new column to your DataFrame.

Calculation Cavalcade To Pandas DataFrame

Let's take a await at the v ways you can add together a column to your DataFrame. For examples of these, bank check out the code below.

Declare new column by referencing a new name

95% of the time you'll be calculation a new column to your dataset past referencing a column name that isn't already there.

You can add a scalar (a single value) or a list (Series, dict, etc.) of items. Make sure if you add a listing it is the aforementioned length every bit your df.

This method will put the new column at the finish of your DataFrame (last column).

                df['new_column_name'] = 5 # You'll get a column of all 5s df['new_column_name'] = [1,2,3,4] # Bold your df is 4 items              

Using df.insert()

Insert will put a new cavalcade in your DataFrame at a specified location. The main advantage is y'all go to pick where in your DataFrame you lot want the column.

                df.insert(loc=column_location,           column='new_column_name',           value=new_column_values)              

Using df.assign()

Assign will also add new columns to your DataFrame, but this time, you lot tin add multiple columns. The entire DataFrame volition exist returned.

                df.assign(new_column=lambda x: x.another_column + vii)              

Using A Dictionary

One of the virtually straight forward ways is to merely use a dictionary. This new dict will add new rows based off of the key values you pass.

                people_dict = {'bob': 'boy', 'Mike': 'boy',             'Katie': 'daughter', 'Stacey': 'girl'}   df['people'] = people_dict              

Using .loc[]

Not recommended, effort one of the above methods first.

You could add together a new cavalcade via the .loc[] methods. This is generally used for information look ups.

                df.loc[:,'new_column'] = new_column_series              

Here'due south a Jupyter notebook with a few examples:

Pandas Add New DataFrame Cavalcade¶

Permit's run through 5 different ways to add together a new column to a Pandas DataFrame

  1. By declaring a new cavalcade proper name with a scalar or listing of values
  2. By using df.insert()
  3. Using df.assign()
  4. Using a lexicon
  5. Using .loc[]

Starting time, allow'due south create our DataFrame

In [29]:

                                            df                      =                      pd                      .                      DataFrame                      ([(                      'Strange Cinema'                      ,                      'Restaurant'                      ,                      289.0                      ),                      (                      'Liho Liho'                      ,                      'Eating place'                      ,                      224.0                      ),                      (                      '500 Order'                      ,                      'bar'                      ,                      80.5                      ),                      (                      'The Square'                      ,                      'bar'                      ,                      25.30                      )],                      columns                      =                      (                      'name'                      ,                      'type'                      ,                      'AvgBill'                      )                      )                      df                    

Out[29]:

proper name type AvgBill
0 Foreign Movie theater Restaurant 289.0
1 Liho Liho Restaurant 224.0
2 500 Club bar fourscore.5
iii The Foursquare bar 25.3

one. Declaring a new column proper noun with a scalar or list of values¶

The easiest way to create a new cavalcade is to only write one out! So assign either a scalar (unmarried value) or a list of items to it.

Out[xxx]:

name blazon AvgBill Day
0 Foreign Cinema Eatery 289.0 Mon
1 Liho Liho Restaurant 224.0 Mon
2 500 Club bar 80.five Monday
3 The Square bar 25.iii Monday

In [31]:

                                            df                      [                      'Solar day'                      ]                      =                      [                      'Monday'                      ,                      'Tuesday'                      ,                      'Wednesday'                      ,                      'Thursday'                      ]                      df                    

Out[31]:

name type AvgBill Day
0 Foreign Cinema Eatery 289.0 Monday
one Liho Liho Eating place 224.0 Tuesday
2 500 Order bar 80.5 Wed
3 The Square bar 25.three Thursday

2. Using df.insert()¶

.insert() will do what information technology sounds like...insert a new cavalcade to your DataFrame. The overnice part is you get to option where you cavalcade appears

In [32]:

                                            df                      .                      insert                      (                      loc                      =                      1                      ,                      column                      =                      "Stars"                      ,                      value                      =                      [                      2                      ,                      2                      ,                      3                      ,                      iv                      ])                      df                    

Out[32]:

name Stars blazon AvgBill Day
0 Strange Cinema ii Restaurant 289.0 Monday
1 Liho Liho two Eating place 224.0 Tuesday
two 500 Club iii bar fourscore.v Wednesday
3 The Square iv bar 25.3 Thursday

3. Using df.assign()¶

.assign() is a scrap like .insert, simply y'all tin pass multiple

In [33]:

                                            df                      .                      assign                      (                      AvgHalfBill                      =                      lambda                      10                      :                      x                      .                      AvgBill                      /                      2                      )                    

Out[33]:

name Stars type AvgBill Day AvgHalfBill
0 Strange Cinema 2 Eating house 289.0 Monday 144.fifty
i Liho Liho 2 Eating place 224.0 Tuesday 112.00
2 500 Club 3 bar 80.5 Wednesday xl.25
iii The Square 4 bar 25.3 Thursday 12.65

iv. Passing a dictionary to your DataFrame¶

Y'all can likewise laissez passer a dictionary to your DataFrame. The keys of the dictionary will become the new values of your column. Notice how the last entry "Square" does not match what is in the 'proper name' column. This is ok and pandas will insert the value past the lodge they are in the dictionary.

In [35]:

                                            df                      [                      'Month'                      ]                      =                      {                      'Jan'                      :                      'Foreign Cinema'                      ,                      'Feb'                      :                      'Liho Liho'                      ,                      'Apr'                      :                      '500 Club'                      ,                      'Dec'                      :                      'Square'                      }                      df                    

Out[35]:

proper noun Stars type AvgBill Solar day Month
0 Strange Cinema two Restaurant 289.0 Monday Jan
1 Liho Liho ii Eatery 224.0 Tuesday Feb
2 500 Club 3 bar eighty.five Wednesday Apr
iii The Foursquare 4 bar 25.iii Thursday Dec

5. Using .loc[]¶

Non recommended, at that place are other (and faster) ways to insert a new column, simply oh well, pick your poison! Endeavor one of the other ways first

In [36]:

                                            df                      .                      loc                      [:,                      "Year"                      ]                      =                      [                      2019                      ,                      2020                      ,                      1995                      ,                      1990                      ]                      df                    

Out[36]:

name Stars blazon AvgBill Twenty-four hour period Month Year
0 Foreign Cinema two Restaurant 289.0 Monday Jan 2019
1 Liho Liho 2 Restaurant 224.0 Tuesday Feb 2020
two 500 Order 3 bar 80.5 Wednesday Apr 1995
3 The Foursquare 4 bar 25.3 Th Dec 1990

Bank check out more Pandas functions on our Pandas Page