Pandas is a python package for manipulating dataframes. You can do many tasks using it like reading, export CSV file as well as converting NumPy array to dataframe. Suppose you have already created dataframe and want to add or append a new row then how you can do so. In this entire tutorial, you will know how to insert a new row in pandas using various methods.
Methods to Insert a New Row in Pandas
In this section, you will know all the various method to insert a new row in the pandas dataframe.
Please note that I am doing all the coding works on Jupyter Notebook. Therefore you do the same for better understanding.
Method 1: Using the index or location of the row
In this method, I will use the index location to insert a new row in pandas. For example, I want to add a new row at the end of the rows then I will execute the following lines of code.
Before adding, Output of the dataframe
import numpy as np
import pandas as pd
import datetime
data = {
"country":["USA","UK","INDIA","CHINA"],
"capital":["Washington, D.C.","London","New Delhi","Beijing"],
"dialing_code":[1,44,91,86]
}
df = pd.DataFrame(data)
print(df)
You will get the following output

Output after adding a new row
df.loc[len(df.index)] = ["Australia","Canberra",61]
print(df)

Here len(df.index ) will find the length of the rows in the existing dataframe.
Method 2: Adding new row using the pd.concat() function
The second method to add new row to the existing dataframe is the pandas.concat() function. Here you have to pass the two dataframe as an argument. One is the existing dataframe and the other the dataframe you want to add. Run the below lines of code and see the output.
import numpy as np
import pandas as pd
import datetime
data = {
"country":["USA","UK","INDIA","CHINA"],
"capital":["Washington, D.C.","London","New Delhi","Beijing"],
"dialing_code":[1,44,91,86]
}
df1 = pd.DataFrame(data)
df2 = pd.DataFrame([["Australia","Canberra",61]],columns=df1.columns)
final_df = pd.concat([df1,df2])
print(final_df)
Output

Method 3: Insert a New Row in using the DataFrame.append() function
The third method to insert a new row in dataframe is using the Dataframe.append() function. In it, you have to use the dot operator with the existing dataframe and pass the new dataframe inside the square bracket[]. It will append a row at the end of the row. Just execute the below lines of code and see the output.
import numpy as np
import pandas as pd
import datetime
data = {
"country":["USA","UK","INDIA","CHINA"],
"capital":["Washington, D.C.","London","New Delhi","Beijing"],
"dialing_code":[1,44,91,86]
}
df1 = pd.DataFrame(data)
df2 = pd.DataFrame([["Australia","Canberra",61]],columns=df1.columns)
final_df = df1.append(df2)
print(final_df)
Output

You can see in the above figure the new row has been inserted.
Conclusion
Sometimes we require to insert new rows inside the existing CSV file as per requirement. These are the methods to insert a new row into the existing dataframe. I hope you have liked this tutorial and this tutorial has solved your queries. If you have any other query then you can contact us for more help.
Source:
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.