{"id":924,"date":"2023-12-11T17:00:00","date_gmt":"2023-12-11T17:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=924"},"modified":"2023-12-13T07:05:36","modified_gmt":"2023-12-13T07:05:36","slug":"pandas-add-rows","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-add-rows\/","title":{"rendered":"How to Add Rows in Pandas DataFrame? (with code)"},"content":{"rendered":"\n<p>One common task in data analysis with Pandas is adding or inserting a new row to an existing DataFrame. Whether you need to add a single row or multiple rows, Pandas provides a lot of techniques for this.\u00a0 In this article, we will explore different techniques for adding rows to a Pandas DataFrame using Pandas.<\/p>\n\n\n\n<p>Let us start by exploring the various ways we can add a row to a DataFrame.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Add Single Row in Pandas DataFrame?<\/strong><\/h2>\n\n\n\n<p>There are various scenarios where adding rows becomes necessary, such as appending new records to an existing dataset or incorporating new observations into an analysis.<\/p>\n\n\n\n<p>We can add a single row to a DataFrame using the following methods:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using DataFrame.loc<\/strong><\/h3>\n\n\n\n<p>One way to add a single row to a DataFrame is by using the DataFrame.loc method. This method allows us to specify the position where the new row should be inserted.\u00a0<\/p>\n\n\n\n<p>To add a row at the end of the DataFrame, we can simply utilize the length of the index of the DataFrame to determine the position.<\/p>\n\n\n\n<p>Let us try an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create a DataFrame\ndf = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Add a new row\ndf.loc[len(df.index)] = ['Amy', 89, 93]\n\n# Display the New DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n\nNew DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n4      Amy     89       93\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using DataFrame.append<\/strong><\/h3>\n\n\n\n<p>Another method to add a single row to a DataFrame is by using the DataFrame.append function. This method allows us to append a new row as a dictionary or a Series.\u00a0<\/p>\n\n\n\n<p>We can set the ignore_index parameter to True, and it will automatically assign a new index to the appended row.<\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create a DataFrame\ndf = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Create a new row as a dictionary\nnew_row = {'Name': 'Amy', 'Maths': 89, 'Science': 93}\n\n# Append the new row to the DataFrame\ndf = df.append(new_row, ignore_index=True)\n\n# Display the New DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n\nNew DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n4      Amy     89       93\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Add Multiple Rows in a DataFrame?<\/strong><\/h2>\n\n\n\n<p>We can also add multiple rows to our DataFrame. The need to add multiple rows can arise when we want to merge two DataFrames.<\/p>\n\n\n\n<p>We can use the <a href=\"https:\/\/favtutor.com\/articles\/concatenate-dataframes-pandas\/\">pandas.concat<\/a> function to join or merge two DataFrames. <strong>This concat method involves creating a new DataFrame containing all the rows that need to be added, and then concatenating it with the original DataFrame.<\/strong><\/p>\n\n\n\n<p>Here is how to do it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create the original DataFrame\ndf1 = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df1)\n\n# Create a new DataFrame with the rows to be added\ndf2 = pd.DataFrame({'Name':['Amy', 'Maddy'], 'Maths':[89, 90], 'Science':[93, 81] })\n\n# Concatenate the two DataFrames\ndf3 = pd.concat([df1, df2], ignore_index=True)\n\n# Display the New DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n\nNew DataFrame:\n       Name  Maths  Science\n0   Martha     87       83\n1      Tim     91       99\n2      Rob     97       84\n3  Georgia     95       76\n4      Amy     89       93\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we explored the various methods we can use to add a single or multiple rows to a Pandas DataFrame. By leveraging the capabilities of Pandas, we can easily add new rows to our DataFrame and incorporate additional data into our analyses. Whether we need to append a single observation or insert multiple records, Pandas offers flexible solutions to meet our needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to add single or multiple rows in a Pandas DataFrame using concat and append, with examples in Python.<\/p>\n","protected":false},"author":10,"featured_media":926,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37,54],"class_list":["post-924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas","tag-pandas-dataframe"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/924\/revisions\/957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/926"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}