In this post, we will learn How to add row to existing DataFrame Pandas or how to append row to existing dataframe with examples.The Pandas dataframe Appen(),loc[] and concat() method is used. To use the Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.
1. How to add row to existing DataFrame Pandas using append()
The Pandas Append() method appends rows at the end of the existing dataframe. It does not change the original dataframe but instead returns a new object. In this example, a new row is initialized as a Python dictionary(new_row), and appending the end of the data frame using the append() method. It is mandatory to pass ignore_index=True, otherwise by setting ignore_index=False, is an error. “TypeError(“Can only append a dict if ignore_index=True”) TypeError: Can only append a dict if ignore_index=True“
import pandas as pd
mydict = {'Name':['Dev','Jack','Rack'],'Subj':['comp','Math','Phy'],'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
#append a new row to existing dataframe
new_row = {'Name':'Dev','Subj':'comp','Marks':100}
df_stud = df_stud.append(new_row, ignore_index=True)
print(f'\nDatafram after append :\n{df_stud}')
Output
Original Dataframe:
Name Subj Marks
0 Dev comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Subj Marks
0 Dev comp 100
1 Jack Math 100
2 Rack Phy 98
3 Dev comp 100
- Pandas Create empty Dataframe and append
- Split Pandas DataFrame by rows and columns
- Split Pandas DataFrame column by Multiple delimiters
- Add one or multiple columns to Pandas DataFrame
2. How to add row to existing DataFrame Pandas using loc[]
The loc() attribute of pandas dataframe is used to access a group of rows and columns by labels or boolean array, We can use DataFrame.loc() to access a particular cell by row index and column label. We can get the index by using the len(dataframe. index) method at which we have to insert a new row.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
#append a new row to existing dataframe
new_row = ['Dev','comp',100]
df_stud.loc[len(df_stud.index)] = new_row
print(f'\nDatafram after append :\n{df_stud}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
3 Dev comp 100
3. How to add row to existing DataFrame Pandas using concat()
The pandas concat() method “performs concatenation operations along an axis while performing optional set logic (union or intersection). In this example, we have data frames df_stud and df_stud2.
- The python dictionaries df_stud ,df_stud2) are convert into dataframes
- The pd.concat() method takes first agrument a list of dataframe
- and second agrument is ignore_index =True
- The result dataframe contains concatenated data from both data frames.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
Dict_data = {'Name':['Tom', 'Marry'],
'Marks':[100, 98],
'Subj':['Phy', 'CHEM']
}
df_stud2 = pd.DataFrame(Dict_data)
res_df = pd.concat([df_stud, df_stud2], ignore_index = True)
res_df.reset_index()
print(f'\nDatafram after append :\n{res_df}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
3 Tom Phy 100
4 Marry CHEM 98
4. How to append a series as a row to an existing DataFrame using append()
In this example, we are appending a series object(ser_obj) in the dataframe to add a new row to DataFrame by flowing these step
- The python dictionaries df_stud,df_stud2) are converted into data frames
- Created a series from the list of values using pd. series() method
- The dataframe append() method is used to append the series as a row to the end of the existing dataframe.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
ser_obj = pd.Series( ['Mack', 'Math', 100],
index=df_stud.columns )
result_df = df_stud.append(ser_obj,
ignore_index=True)
print(f'\nDatafram after append :\n{result_df}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
3 Mack Math 100