{"id":7382,"date":"2025-03-21T06:00:00","date_gmt":"2025-03-21T06:00:00","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=7382"},"modified":"2025-03-21T10:39:14","modified_gmt":"2025-03-21T10:39:14","slug":"how-to-insert-new-column-to-pandas-dataframe","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/","title":{"rendered":"How to Insert New Column to Pandas DataFrame"},"content":{"rendered":"\n<p>Pandas dataframes are very useful to store, manipulate and analyze data in a tabular manner, as rows and columns. They allow you to perform numerous data operations to easily retrieve the information you need. Often Python developers need to add one or more new columns to an existing dataframe. There are several ways ways to do it. In this article, we will learn the different ways to insert new column to pandas dataframe.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#How_to_Insert_New_Column_to_Pandas_DataFrame\" >How to Insert New Column to Pandas DataFrame<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#1_Using_List_As_Column\" >1. Using List As Column<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#2_Using_Assign_function\" >2. Using Assign() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#3_Using_Insert_Method\" >3. Using Insert() Method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#4_Using_loc_method\" >4. Using loc() method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#5_Using_Dictionary\" >5. Using Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#6_Add_Derived_Columns\" >6. Add Derived Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#7_Add_Column_from_Another_Dataframe\" >7. Add Column from Another Dataframe<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#8_Adding_Multiple_Columns\" >8. Adding Multiple Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Insert_New_Column_to_Pandas_DataFrame\"><\/span>How to Insert New Column to Pandas DataFrame<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are some of the most common ways to add or insert new column to a Pandas Dataframe. Let us say you have the following dataframe of 4 students with name and id values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br><br>data = {'Name': ['John', 'Jane', 'Jim', 'Joe'],<br>        'ID': [1, 2, 3, 4]}<br><br>df = pd.DataFrame(data)<br><br>print(df)<br><br>## output<br><br>   Name  ID<br>0  John   1<br>1  Jane   2<br>2   Jim   3<br>3   Joe   4<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_List_As_Column\"><\/span>1. Using List As Column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us say you want to add a new column to the above dataframe, with age values for the students. You can do this by first storing the column values as a list. Then we assign this list to the new column in dataframe, using the syntax <em>dataframe[new_column_name] = column_values_list<\/em>. Here is the code to add age column to our dataframe. The age values of 4 students is stored in a list and assigned to the column.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age = [16,15,17,14]<br>df['age'] = age<br>print(df)<br><br>## output<br><br>   Name  ID  age<br>0  John   1   16<br>1  Jane   2   15<br>2   Jim   3   17<br>3   Joe   4   14<\/pre>\n\n\n\n<p>The column values can be of different data type, it will still work. Here is an example, where some values are numbers while some are text. The following code will give the same output as above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age = [16,'15',17,'14']<br>df['age'] = age<br>print(df)<\/pre>\n\n\n\n<p>Please note, the number of column values should be same as the length of index column, otherwise it will give an error. Here is an example where we add 3 column values whereas our dataframe has 4 index values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age = [16,15,17]<br>df['age'] = age<br>print(df)<br><br>## output<br><br>ValueError: Length of values (3) does not match length of index (4)<\/pre>\n\n\n\n<p>Also, when we add a new column, it will modify the existing dataframe instead of creating its copy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_Assign_function\"><\/span>2. Using Assign() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use assign() method available for every dataframe to add a new column. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dataframe.assign(column_name=list_of_column_values)<\/pre>\n\n\n\n<p>You can call this function directly on dataframe variable. In it, we specify the column name, along with a list of column values.<\/p>\n\n\n\n<p>Here is an example to add age column to our dataframe.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age_data = [16,15,17,14]<br>df=df.assign(age=age_data)<br>print(df)<br><br>## output<br><br>   Name  ID  age<br>0  John   1   16<br>1  Jane   2   15<br>2   Jim   3   17<br>3   Joe   4   14<\/pre>\n\n\n\n<p>Please note, using assign() method will return a new dataframe, without changing the original dataframe. If you want to modify the existing dataframe, then you need to re-assign the output of assign() method back to the original dataframe variable, as shown above.<\/p>\n\n\n\n<p>In this case also, if the number of items provided as column values is more or less than the length of dataframe index, then it will give an error. It has to exactly equal to the length of dataframe index.<\/p>\n\n\n\n<p>Please note, in the above code, we have mentioned column name without any quotes. If you include single or double quotes in your column name, you will get the following error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df=df.assign('age'=age_data)<br>OR<br>df=df.assign(\"age\"=age_data)<br><br>## error output<br><br>SyntaxError: expression cannot contain assignment, perhaps you meant \"==\"?<\/pre>\n\n\n\n<p>Here is an example where our column &#8216;age bar&#8217; has a space in it and we mention it without quotes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df=df.assign( <strong>age bar<\/strong>=age_data)<br><br>## output<br>SyntaxError: invalid syntax. Perhaps you forgot a comma?<\/pre>\n\n\n\n<p>So if your new column has a space in it, you cannot wrap it in quotes, or mention it as it is. If your new column name has a space in it then you need to provide the column name-values as a dictionary along with unpacking (**) operator at its beginning.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age_data = [16,15,17,14]<br>df=df.assign(<strong>**{'age bar':age_data}<\/strong>)<br>print(df)<br><br>## output<br><br>   Name  ID  age bar<br>0  John   1       16<br>1  Jane   2       15<br>2   Jim   3       17<br>3   Joe   4       14<\/pre>\n\n\n\n<p>In fact, you need to use this unpacking operator, if you want to add multiple columns to your dataframe. Here is an example to add two columns &#8211; age and city to our dataframe.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data={\"age\": [16,15,17,14],<br>      \"city\":['NYC','Mumbai','Tokyo','SFO']}<br>df=df.assign(**data)<br>print(df)<br><br>## output<br><br>   Name  ID  age   city<br>0  John   1   16    NYC<br>1  Jane   2   15  Mumbai<br>2   Jim   3   17  Tokyo<br>3   Joe   4   14    SFO<\/pre>\n\n\n\n<p>In the above code, first we create a dictionary containing the key-value pairs of both columns. Here each key is the column name and value is the list of column values for that column. Then we pass it to assign function prefixing it with unpacking operator.<\/p>\n\n\n\n<p>So this is a useful method to one or more columns using a dictionary. Typically we receive data as JSON\/dictionary. This method can be used to easily insert this data into a dataframe using assign method.<\/p>\n\n\n\n<p>Another easy way to add multiple columns using assign function is to mention each column_name=column_value_list in a comma-separated manner.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df=df.assign(age=[16,15,17,14],city=['NYC','Mumbai','Paris','Tokyo'])<br>print(df)<br><br>## output<br>   Name  ID  age    city<br>0  John   1   16     NYC<br>1  Jane   2   15  Mumbai<br>2   Jim   3   17   Paris<br>3   Joe   4   14   Tokyo<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_Insert_Method\"><\/span>3. Using Insert() Method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Both the above solutions will add new column or columns at the end of dataframe. What if you you want to insert one or more columns at a specific position of your dataframe? In this case, you need to use insert() method. It modifies the original dataframe without creating its copy. It provides many useful options to add columns at different positions of dataframe. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dataframe.insert(position, column_name, column_values, allow_duplicates = False) <\/pre>\n\n\n\n<p>You can call insert() function from every dataframe variable. The first argument is the position where you want to insert the new column, starting 0, 1, 2&#8230;. Next argument is the column name, followed by a list of column values. Lastly, you can specify if you want to allow duplicate column names or not.<\/p>\n\n\n\n<p>Here is an example to insert age column before <em>id<\/em> and after <em>name<\/em> columns.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.insert(1, \"age\", [16,15,17,14], True)<br>print(df)<br><br>## output<br><br>   Name  age  ID<br>0  John   16   1<br>1  Jane   15   2<br>2   Jim   17   3<br>3   Joe   14   4<\/pre>\n\n\n\n<p>When you insert a new column at a given position, it will shift the existing column at that position to its right.<\/p>\n\n\n\n<p>Now, if you want to call insert function again, then you need to use the new position numbers. Here is an example to insert city column before ID column. Here we have specified location=2 whereas in the above code, it was location=1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.insert(2, \"city\", ['NYC','Mumbai','Tokyo','Paris'], True)<br>print(df)<br><br>## output<br><br>   Name  age   city  ID<br>0  John   16    NYC   1<br>1  Jane   15  Mumbai  2<br>2   Jim   17  Tokyo   3<br>3   Joe   14  Paris   4<\/pre>\n\n\n\n<p>If you want to prepend a new column at the beginning of the dataframe, use position=0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.insert(0, \"age\", [16,15,17,14], True)<br>print(df)<br><br>## output<br><br>   age  Name  ID<br>0   16  John   1<br>1   15  Jane   2<br>2   17   Jim   3<br>3   14   Joe   4<\/pre>\n\n\n\n<p>If you want to add a new column at the end of dataframe, use position as len(df.columns). This will calculate the number of columns and use this value as position.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.insert(len(df.columns), \"age\", [16,15,17,14], True)<br>print(df)<br><br>## output<br>   Name  ID  age<br>0  John   1   16<br>1  Jane   2   15<br>2   Jim   3   17<br>3   Joe   4   14<\/pre>\n\n\n\n<p>As you can see, insert() makes it easy to add new column at different positions of dataframe.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_loc_method\"><\/span>4. Using loc() method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use loc() function available for every dataframe, to insert new column in pandas dataframe. It is a very useful function that not only allows you to insert or modify column at a given position but also allows you to create new columns whose values are derived from other columns. Here is the syntax to add new column using loc method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dataframe.loc[:,column_name] = list_of_column_values<\/pre>\n\n\n\n<p>Here is an example to add a new column using loc.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.loc[:,'age'] =[16,'15',17,'14']<br>print(df)<br><br>## output<br>   Name  ID age<br>0  John   1  16<br>1  Jane   2  15<br>2   Jim   3  17<br>3   Joe   4  14<\/pre>\n\n\n\n<p>You can also use it to create a new column based on values derived from other column in dataframe. Here is an example to create a new column based on ID column value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.loc[df['ID'] &gt;= 3, 'Category'] = 'Tall'<br>df.loc[df['ID'] &lt; 3, 'Category'] = 'Short'<br>print(df)<br><br>## output<br><br>   Name  ID Category<br>0  John   1    Short<br>1  Jane   2    Short<br>2   Jim   3     Tall<br>3   Joe   4     Tall<\/pre>\n\n\n\n<p>In the above code, we have provided 2 arguments to loc method. The first method is the condition and the second argument is the column name. We have assigned it values Tall\/Short depending on the value of ID column.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_Dictionary\"><\/span>5. Using Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this solution, we use a dict, along with map function, to add one or more columns to a dataframe. First we define the dict where the key-value pairs are such that all key values belong to one of the existing column values, and all values are equal to the new column&#8217;s values. In this example, each key is a value for column <em>name<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">address = {'John': 'NewYork', 'Joe': 'Chicago', <br>            'Jane': 'Boston', 'Jim': 'Miami'}<\/pre>\n\n\n\n<p>Next, we call map function on this column, and assign its result to a new column <em>Address<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['Address'] = df['Name'].map(address)<br>print(df)<br><br>## output<br><br>   Name  ID  Address<br>0  John   1  NewYork<br>1  Jane   2   Boston<br>2   Jim   3    Miami<br>3   Joe   4  Chicago<\/pre>\n\n\n\n<p>In the above dataframe, you will see that although our original dict contained key-value pairs in random order, each value has been mapped to its corresponding Name column value. For example, if Name=Jane, then Address=Boston, and so on. This is a great way to add column from other data source where the data may be present as JSON or dict, and the order of key-value pairs may not be same as the order of column values in your dataframe.<\/p>\n\n\n\n<p>Please note, if there is no key-value pair matching the column value of your dataframe, then the new column&#8217;s value will be NaN. In the following example, the dict has only 3 key-value pairs but the dataframe has 4 rows. So one value of Address column is NaN.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">address = {'John': 'NewYork', 'Joe': 'Chicago', <br>            'Jane': 'Boston'}<br><br><br>df['Address'] = df['Name'].map(address)<br>print(df)<br><br>## output<br><br>   Name  ID  Address<br>0  John   1  NewYork<br>1  Jane   2   Boston<br>2   Jim   3      <strong>NaN<\/strong><br>3   Joe   4  Chicago<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Add_Derived_Columns\"><\/span>6. Add Derived Columns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes you may need to create a new column based on value of another column. There are several ways to do this. One of the simplest ways to do this is using mathematical operators. Here is an example to create a new column new_id using values of column ID.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['new_ID'] = df['ID'] + 100<br>print(df)<br><br>## output<br><br>   Name  ID  new_ID<br>0  John   1     101<br>1  Jane   2     102<br>2   Jim   3     103<br>3   Joe   4     104<\/pre>\n\n\n\n<p>As we have seen earlier, you can also use loc method to create a new column based on condition applied to the values of another existing column.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.loc[df['ID'] &gt;= 3, 'Type'] = 'Old'<br>df.loc[df['ID'] &lt; 3, 'Type'] = 'Young'<br>print(df)<br><br>## output<br><br>   Name  ID Category<br>0  John   1    Young<br>1  Jane   2    Young<br>2   Jim   3     Old<br>3   Joe   4     Old<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Add_Column_from_Another_Dataframe\"><\/span>7. Add Column from Another Dataframe<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Another common requirement is to be able to insert a new column in a dataframe from another dataframe. You can easily to this using assignment operator &#8216;=&#8217;.<\/p>\n\n\n\n<p>First, we define the second dataframe.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df2 = pd.DataFrame({'Marks': [70, 80, 90, 60]})<\/pre>\n\n\n\n<p>Next, we use assignment operator to assign the values of Marks column in df2 dataframe to the new Marks column in df dataframe.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['Marks'] = df2['Marks']<br>print(df)<br><br>## output<br>   Name  ID  Marks<br>0  John   1   70.0<br>1  Jane   2   80.0<br>2   Jim   3   90.0<br>3   Joe   4   60.0<\/pre>\n\n\n\n<p>Please note, the dataframe df did not have Marks column earlier, but was created during assignment of values from df2. The column values of new column will have same order as they are in the other dataframe. If the number of values in the other dataframe is less than the number of rows in our original dataframe, then the absent values will populated as NaN. Here is an example, where our second dataframe has only 3 values whereas our original dataframe has 4 rows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df2 = pd.DataFrame({'Marks': [70, 80, 90]})<br>df['Marks'] = df2['Marks']<br>print(df)<br><br>## output<br><br>   Name  ID  Marks<br>0  John   1   70.0<br>1  Jane   2   80.0<br>2   Jim   3   90.0<br>3   Joe   4    NaN<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_Adding_Multiple_Columns\"><\/span>8. Adding Multiple Columns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to add multiple columns, then the easiest way is to create a dictionary with column names and values. In this dictionary, each key is the column name and and its value is a list of column values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data={\"age\": [16,15,17,14],<br>      \"city\":['NYC','Mumbai','Tokyo','SFO']}<\/pre>\n\n\n\n<p>Once you have defined this dictionary, then you can call assign() function on dataframe, and pass this dictionary variable prefixed with unpacking &#8216;**&#8217; operator, as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df=df.assign(**data)<br>print(df)<br><br>## output<br><br>   Name  ID  age   city<br>0  John   1   16    NYC<br>1  Jane   2   15  Mumbai<br>2   Jim   3   17  Tokyo<br>3   Joe   4   14    SFO<\/pre>\n\n\n\n<p>Alternatively, you can call assign function for your dataframe and pass the column_name-column_value pairs in a comma-separated manner.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df=df.assign(age=[16,15,17,14],city=['NYC','Mumbai','Paris','Tokyo'])<br>print(df)<\/pre>\n\n\n\n<p>This code will give the same output as above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt several different ways to insert new column in Pandas dataframe. We learnt how to add column using various methods. We also learnt how to add multiple columns. We have also seen how to derive column from another dataframe column, as well as create new column using column from another dataframe. You can use any of these methods as per your requirement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. How to Add Single column to dataframe?<\/strong><\/p>\n\n\n\n<p>You can add new column by directly assigning its values as a list to the dataframe. You can also use insert, loc or assign for this purpose.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br>df = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['John','Jane','Jim']})<br># Adding a new column 'Marks'<br>df['Marks'] = [70, 80, 90]<\/pre>\n\n\n\n<p><strong>2. How to Add Multiple columns to dataframe<\/strong><\/p>\n\n\n\n<p>You can add multiple columns to dataframe using assign() method. You can either provide column name-value pairs in a comma separated manner as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br>df = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['John','Jane','Jim']})<br># Adding new columns Marks and Age<br>df.assign(Marks=[70, 80, 90],Age=[15,14,16])<\/pre>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-order-of-dataframe-columns-in-python\/\">How to Change Order of Dataframe Columns<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-select-multiple-columns-in-pandas-dataframe\/\">How to Select Multiple Columns in Pandas Dataframe<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-randomly-select-item-from-python-list\/\">How to Randomly Select Item from Python List<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.<\/p>\n","protected":false},"author":1,"featured_media":7421,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[399],"class_list":["post-7382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-add-column"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Insert New Column to Pandas DataFrame - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Insert New Column to Pandas DataFrame - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-21T06:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-21T10:39:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Insert New Column to Pandas DataFrame\",\"datePublished\":\"2025-03-21T06:00:00+00:00\",\"dateModified\":\"2025-03-21T10:39:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\"},\"wordCount\":1782,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"add column\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\",\"name\":\"How to Insert New Column to Pandas DataFrame - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2025-03-21T06:00:00+00:00\",\"dateModified\":\"2025-03-21T10:39:14+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"insert new column in pandas dataframe\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Insert New Column to Pandas DataFrame\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Insert New Column to Pandas DataFrame - Ubiq BI","description":"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/","og_locale":"en_US","og_type":"article","og_title":"How to Insert New Column to Pandas DataFrame - Ubiq BI","og_description":"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-03-21T06:00:00+00:00","article_modified_time":"2025-03-21T10:39:14+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Insert New Column to Pandas DataFrame","datePublished":"2025-03-21T06:00:00+00:00","dateModified":"2025-03-21T10:39:14+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/"},"wordCount":1782,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1","keywords":["add column"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/","name":"How to Insert New Column to Pandas DataFrame - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1","datePublished":"2025-03-21T06:00:00+00:00","dateModified":"2025-03-21T10:39:14+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often Python developers need to add one or more columns to Pandas dataframe. Here is how to insert new column to Pandas dataframe.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"insert new column in pandas dataframe"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-insert-new-column-to-pandas-dataframe\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Insert New Column to Pandas DataFrame"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/03\/insert-new-column-pandas-dataframe.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1V4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=7382"}],"version-history":[{"count":46,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7382\/revisions"}],"predecessor-version":[{"id":7430,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7382\/revisions\/7430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/7421"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=7382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=7382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=7382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}