How to Add a New Row to a Pandas Dataframe Object in Python 


               


How to Add a New Row to a Pandas Dataframe Object in Python



Python


In this article, we show how to add a new row to a pandas dataframe object in Python.

So if you have an existing pandas dataframe object, you are free to do many different modifications, including adding columns or rows to the dataframe object, deleting columns or rows, updating values, etc.

We will show in this article how you can add a new row to a pandas dataframe object in Python.

So we below we create a dataframe object that has rows, 'A', 'B', 'C', and 'D'

We will then add a new row, 'E', to this dataframe objection.

Adding a new row to a pandas dataframe object is relatively simple. You just declare the row and set it equal to the values that you want it to have. And that's all.

Adding a new row to a pandas dataframe object is shown in the following code below.



So let's now go over the code.

So we first have to import the pandas module. We do this with the line, import pandas as pd.

as pd means that we can reference the pandas module with pd instead of writing out the full pandas each time.

We import rand from numpy.random, so that we can populate the DataFrame with random values. In other words, we won't need to manually create the values in the table. The randn function will populate it with random values.

We create a variable, dataframe1, which we set equal to, pd.DataFrame(randn(4,3),['A','B','C','D',],['X','Y','Z'])

This creates a DataFrame object with 4 rows and 3 columns.

The rows are 'A', 'B', 'C', and 'D'.

The columns are 'W', 'X', and 'Y'.

After we output the dataframe1 object, we get the DataFrame object with all the rows and columns, which you can see above.

We then add a new row to this dataframe1 object.

This new row has a label of 'E' and contains the values, 3.0,7.0, and 11.0.

When we then output the dataframe1 object, we now have the original dataframe1 object with the addition of the 'E' row.

So you can see that we have now successfully added a new row to the dataframe object.

Be careful that when you add a new row to the dataframe object that the number of values you are adding to the row is equal to the number of columns there are. If you try to create a row and insert 2 values and there are 3 columns, there will be an error showing that the values entered is mismatched with the number of rows. So you must be careful to enter in a row with a number of values equivalent to the number of columns there are in the dataframe object.

Below I purposefully created a row with 4 values when there are really 3 columns in the dataframe object.

The error I received is shown below.



So always make sure to create a row that has a number of values equivalent to the number of columns there are in the dataframe object.

And this is how we an add a new row to a pandas dataframe object in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...