. Advertisement .
..3..
. Advertisement .
..4..
Pandas dataframe is no longer a strange name for coders. This two-dimensional structure helps you analyze the data. For precise analysis, a new dataframe with some added rows for records is required.
There are various methods to add rows to the Pandas dataframe. This post will help you get the quickest ways. So let’s see how to append row to dataframe.
What Is Pandas dataframe.append() Function?
As its name suggests, dataframe.append() appends other dataframes’ rows to the given one and returns a new object. New cells and columns will be assigned with NaN value, so you need to add columns not belonging to the original dataframes.
Syntax:
DataFrame.append(other, sort=None, ignore_index=False, verify_integrity=False)
Parameters:
- other: a list of Series/dict-like or Dataframe object
- sort: if the columns are not aligned, the function will sort them. As the default sorting function is deprecated, it is back to not-sorting in the next pandas version. Turn sort=True to silence the sort and warnings. sort=False will not sort and silence all the warnings.
- ignore_index: Don’t use index labels if the result returns True.
- verify_integrity: raise ValueError and use duplicates to create index if True.
How To Append Row To Pandas DataFrame
Append The Second DataFrame To The First One
Code:
# Importing pandas as pd
import pandas as pd
# Creating the first Dataframe using dictionary
df1 = df = pd.DataFrame({"a":[1, 2, 3, 4],
"b":[5, 6, 7, 8]})
# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
"b":[5, 6, 7]})
# Print df1
print(df1, "\n")
# Print df2
df2
After receiving two outputs of two columns, now append the second dataframe to the end of the first one:
# to append df2 at the end of df1 dataframe
df1.append(df2)
a b
0 1 5
1 2 6
2 3 7
3 4 8
0 1 5
1 2 6
2 3 7
In the appended result, the second dataframe’s index value remains unchanged. To maintain this value during the execution, set ignore_index=True
:
# A continuous index value will be maintained
# across the rows in the new appended data frame.
df1.append(df2, ignore_index = True)
Append DataFrame With Different Shapes
If you append a dataframe having a different number of columns, non-existent index values will be assigned with NaN values.
# Importing pandas as pd
import pandas as pd
# Creating the first Dataframe using dictionary
df1 = pd.DataFrame({"a":[1, 2, 3, 4],
"b":[5, 6, 7, 8]})
# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
"b":[5, 6, 7],
"c":[1, 5, 4]})
# for appending df2 at the end of df1
df1.append(df2, ignore_index = True)
Output:
a b c
0 1 5 NaN
1 2 6 NaN
2 3 7 NaN
3 4 8 NaN
4 2 5 1.0
5 2 6 5.0
6 3 7 4.0
Other Methods To Add Row To DataFrame
Concat
concat()
– This method concatenates two dataframes into one object. You can also create a dataframe having a single row and append it to the existing one.
Parameters
- List of dataframes
- ignore_index
axis = 0
: it denotes when converting the dataframe’s row. Useaxis = 1
to concatenate columns.
The function tends to return to you a new dataframe containing the row from two existing ones.
Code:
df2 = pd.DataFrame({'First Name': ['Kumar'],
'Last Name' : ['Ram'],
'Country' : ['India']})
df = pd.concat([df, df2], ignore_index = True, axis = 0)
df
ILOC
iloc
allows you to add a row in your existing dataframe at a specific position. You can also use this function to assign a new row at a preferred one.
However, this will replace the existing row as well. To use ILOC, make sure that your dataframe already has at least one empty row in the position. If not, you will get the IndexError: iloc cannot enlarge its target object error.
df.iloc[1] = ['India', 'Shivam', 'Pandey']
df
Conclusion
Overall, there are various methods to append row to dataframe in Pandas. Using the append()
function is the most common way, but you can also use loc()
and concat()
as well.
Leave a comment