Add Column To Dataframe Pandas

One of the most common Pandas tasks yous'll practise is add more than data to your DataFrame. This means you demand to become an expert at adding a column to your DataFrame.

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

  • By declaring a new column name with a scalar or listing of values
  • Past using df.insert()
  • Using df.assign()
  • Using a dictionary
  • Using .loc[]

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

Adding Column To Pandas DataFrame

Let's take a wait at the 5 ways yous tin can add a column to your DataFrame. For examples of these, cheque out the code below.

Declare new column by referencing a new proper noun

95% of the fourth dimension you'll be adding a new column to your dataset past referencing a cavalcade name that isn't already there.

Yous can add a scalar (a unmarried value) or a list (Series, dict, etc.) of items. Make sure if you add a listing it is the same length as your df.

This method will put the new cavalcade at the stop of your DataFrame (last cavalcade).

                df['new_column_name'] = 5 # You'll become a column of all 5s df['new_column_name'] = [one,2,iii,iv] # Assuming your df is 4 items              

Using df.insert()

Insert will put a new cavalcade in your DataFrame at a specified location. The main reward is yous get to pick where in your DataFrame you want the cavalcade.

                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 can add together multiple columns. The unabridged DataFrame volition be returned.

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

Using A Dictionary

Ane of the most direct frontwards ways is to only use a dictionary. This new dict will add new rows based off of the key values you laissez passer.

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

Using .loc[]

Not recommended, try one of the in a higher place methods get-go.

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

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

Hither's a Jupyter notebook with a few examples:

Pandas Add New DataFrame Column¶

Let's run through 5 different ways to add a new cavalcade to a Pandas DataFrame

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

Get-go, let's create our DataFrame

In [29]:

                                            df                      =                      pd                      .                      DataFrame                      ([(                      'Foreign Cinema'                      ,                      'Restaurant'                      ,                      289.0                      ),                      (                      'Liho Liho'                      ,                      'Restaurant'                      ,                      224.0                      ),                      (                      '500 Club'                      ,                      'bar'                      ,                      80.five                      ),                      (                      'The Square'                      ,                      'bar'                      ,                      25.thirty                      )],                      columns                      =                      (                      'name'                      ,                      'type'                      ,                      'AvgBill'                      )                      )                      df                    

Out[29]:

proper noun type AvgBill
0 Foreign Cinema Eatery 289.0
1 Liho Liho Restaurant 224.0
2 500 Club bar 80.v
3 The Square bar 25.iii

ane. Declaring a new column name with a scalar or list of values¶

The easiest manner to create a new column is to but write one out! Then assign either a scalar (unmarried value) or a list of items to information technology.

Out[30]:

name blazon AvgBill Day
0 Foreign Movie theater Restaurant 289.0 Monday
1 Liho Liho Restaurant 224.0 Mon
2 500 Guild bar 80.five Monday
3 The Square bar 25.3 Monday

In [31]:

                                            df                      [                      'Day'                      ]                      =                      [                      'Mon'                      ,                      'Tuesday'                      ,                      'Wed'                      ,                      'Thursday'                      ]                      df                    

Out[31]:

name blazon AvgBill Day
0 Foreign Movie house Eating place 289.0 Monday
1 Liho Liho Restaurant 224.0 Tuesday
two 500 Club bar eighty.5 Midweek
3 The Foursquare bar 25.iii Thursday

2. Using df.insert()¶

.insert() volition do what it sounds like...insert a new column to your DataFrame. The nice part is yous become to option where you column appears

In [32]:

                                            df                      .                      insert                      (                      loc                      =                      i                      ,                      column                      =                      "Stars"                      ,                      value                      =                      [                      two                      ,                      ii                      ,                      3                      ,                      iv                      ])                      df                    

Out[32]:

proper noun Stars blazon AvgBill Solar day
0 Foreign Cinema two Restaurant 289.0 Mon
i Liho Liho 2 Restaurant 224.0 Tuesday
two 500 Lodge 3 bar lxxx.5 Wed
3 The Square 4 bar 25.3 Thursday

3. Using df.assign()¶

.assign() is a bit like .insert, but you can pass multiple

In [33]:

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

Out[33]:

proper name Stars type AvgBill Day AvgHalfBill
0 Strange Movie theatre two Restaurant 289.0 Monday 144.50
1 Liho Liho 2 Eating house 224.0 Tuesday 112.00
2 500 Society iii bar fourscore.5 Wednesday twoscore.25
3 The Square 4 bar 25.iii Th 12.65

4. Passing a dictionary to your DataFrame¶

You can also pass a dictionary to your DataFrame. The keys of the dictionary volition go the new values of your column. Notice how the last entry "Square" does not match what is in the 'name' column. This is ok and pandas will insert the value by the social club they are in the lexicon.

In [35]:

                                            df                      [                      'Calendar month'                      ]                      =                      {                      'Jan'                      :                      'Strange Movie theatre'                      ,                      'February'                      :                      'Liho Liho'                      ,                      'Apr'                      :                      '500 Society'                      ,                      'December'                      :                      'Foursquare'                      }                      df                    

Out[35]:

proper noun Stars type AvgBill Twenty-four hours Month
0 Foreign Movie theatre 2 Eating place 289.0 Monday January
1 Liho Liho 2 Restaurant 224.0 Tuesday Feb
2 500 Club 3 bar 80.v Wednesday Apr
3 The Square four bar 25.3 Th Dec

5. Using .loc[]¶

Not recommended, in that location are other (and faster) ways to insert a new cavalcade, simply oh well, pick your poison! Effort one of the other means beginning

In [36]:

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

Out[36]:

name Stars blazon AvgBill Solar day Calendar month Year
0 Strange Cinema 2 Eatery 289.0 Monday January 2019
1 Liho Liho 2 Eatery 224.0 Tuesday Feb 2020
2 500 Club 3 bar 80.five Wednesday Apr 1995
three The Square four bar 25.3 Thursday Dec 1990

Bank check out more Pandas functions on our Pandas Page