Add Row to Empty DataFrame Pandas
There are three ways to add a row to the empty Pandas DataFrame. After creating an empty DataFrame with just columns, based on the row index, it is possible to insert rows using the loc[] property. Using the pandas.DataFrame.append() function, the row(s) are appended to the empty DataFrame through the dictionary of values. Also append the entire DataFrame to the empty DataFrame using this function. Similarly, we can achieve this through the pandas.concat() function.
Using Pandas.DataFrame.Append
We use this function to append the rows of another DataFrame to the existing DataFrame. If the columns in the existing DataFrame do not exist, the other DataFrame columns are created in the existing DataFrame.
Syntax:
The following is the syntax of the pandas.DataFrame.append() function.
pandas.DataFrame.append(other, ignore_index, verify_integrity, sort)
- other: This refers to another DataFrame in which the rows of this DataFrame are appended to the existing DataFrame. If you want to append a single row, you need to pass a dictionary of values as the parameter.
- ignore_index (By default = False): This parameter is utilized when you are appending the rows to the DataFrame that already has rows. If it is “False”, the existing rows indices are also appended. If it is “True”, the rows are labelled from 0 to n-1.
- We can check for the duplicate indices using the “verify_integrity” parameter (by default = False). If the indices are duplicate and the verify_integrity is set to True, it returns the “ValueError: Indexes have overlapping values”.
- It is possible to sort the columns if the columns of the existing DataFrame and another DataFrame are not aligned using the “sort” parameter by setting it to “True”. (by default = False).
Example 1: Add a Single Row
Create an empty Pandas DataFrame with four columns – “Campaign_Name”, “Location”, “StartDate”, “Budget”. Using the pandas.DataFrame.append() function, append one row to this DataFrame. The record that we add is {‘Campaign_Name’ :’Technical Camp’,’Location’ :’USA’,’StartDate’ :’12/05/2023′,’Budget’ :2000}.
# Create empty DataFrame - Campaign with 4 columns
Campaign=pandas.DataFrame(columns=['Campaign_Name','Location','StartDate','Budget'])
print(Campaign,"\n")
# Append Single row
Campaign = Campaign.append({'Campaign_Name' :'Technical Camp','Location' :'USA','StartDate' :'12/05/2023','Budget' :2000},ignore_index = True)
print(Campaign,"\n")
Output:
Previously, the DataFrame campaign was empty. After adding one row, it shows the data with index = 0.
Example 2: Add Multiple Rows
Use the same DataFrame that was created under the first example and append three rows at a time using the pandas.DataFrame.append() function.
# Create empty DataFrame - Campaign with 4 columns
Campaign=pandas.DataFrame(columns=['Campaign_Name','Location','StartDate','Budget'])
print(Campaign,"\n")
# Append Multiple rows
Campaign = Campaign.append({'Campaign_Name' :'Technical Camp','Location' :'USA','StartDate' :'12/05/2023','Budget' :2000},ignore_index = True)
Campaign = Campaign.append({'Campaign_Name' :'Marketing camp','Location' :'India','StartDate' :'23/06/2023','Budget':9000},ignore_index = True)
Campaign = Campaign.append({'Campaign_Name' :'MSales camp','Location' :'Italy','StartDate' :'24/01/2023','Budget':1200},ignore_index = True)
print(Campaign,"\n")
Output:
Three rows are added one after another with 0, 1, and 2 indices.
Example 3: Add Rows from Another DataFrame
- Create an empty DataFrame which is labeled as “Campaign1” with two columns.
- Create the “Campaign2” DataFrame with two columns and four records.
- Append “Campaign2” to the “Campaign1” using the pandas.DataFrame.append() function.
# Create empty DataFrame - Campaign1 with 2 columns
Campaign1=pandas.DataFrame(columns=['Campaign_Name','Location'])
print(Campaign1,"\n")
# Create DataFrame - Campaign2 with 2 columns and 4 records
Campaign2=pandas.DataFrame([['java camp','India'],['linux camp','USA'],['c/c++ camp','India'],['python camp','USA']],columns=['Campaign_Name','Location'])
print(Campaign2,"\n")
# Append Campaign2 to the Campaign1
Campaign1 = Campaign1.append(Campaign2)
print(Campaign1)
Output:
After appending the second DataFrame (Campaign2) to the first DataFrame (Campaign1), Campaign1 holds four records. Previously, it was empty.
Using Pandas.DataFrame.Loc[]
We already know that this property is used to select a group of rows and columns using the index label. In the DataFrame, the row index starts with 0 by default if we didn’t pass any index labels to it. We can use this index and assign the data to them. This way, the rows are added to the empty DataFrame.
Syntax:
Let’s see how to specify the row_index within loc[]. The row values are specified in a list and it is assigned to the loc[] property.
pandas.DataFrame.loc[row_index] = [values…]
Example 1: Add a Single Row
Create an empty DataFrame which is labeled as “Campaign” with four columns and add one row using the row index through the loc[] property.
# Create empty DataFrame - Campaign with 4 columns
Campaign=pandas.DataFrame(columns=['Campaign_Name','Location'])
print(Campaign,"\n")
# Append row using loc[]
Campaign.loc[0] = ['java camp','India']
print(Campaign)
Output:
The row is added at the first position since the specified index is 0.
Example 2: Add Multiple Rows
Use the same property and add three rows at 0, 1, and 2 indices, one after another.
# Create empty DataFrame - Campaign with 4 columns
Campaign=pandas.DataFrame(columns=['Campaign_Name','Location'])
print(Campaign,"\n")
# Append rows using loc[]
Campaign.loc[0] = ['java camp','India']
Campaign.loc[1] = ['c/c++ camp','India']
Campaign.loc[2] = ['Linux camp','USA']
print(Campaign)
Output:
Three rows are added to the DataFrame.
Using Pandas.Concat
The pandas.concat() function concatenates two or more DataFrames along with rows or columns. This is controlled using the axis parameter. It is 0 by default.
Syntax:
The following is the syntax of the pandas.concat() function.
pandas.concat([DataFrames,…,] axis, join, ignore_index, verify_integrity, sort, copy,…)
- other: This refers to another DataFrame in which the rows of this DataFrame are appended to the existing DataFrame. If you want to append a single row, you need to pass a dictionary of values as the parameter.
- If axis = 0, the concatenation is done along the rows. Concatenation is done along the columns if it is set to 1.
- join{‘inner’, ‘outer’}: This specifies the type of join (inner join/outer join). By default = “outer”.
- ignore_index (by default = False): This parameter is utilized when you are appending the rows to the DataFrame that already has rows. If it is “False”, the existing rows indices are also appended. If it is “True”, the rows are labelled from 0 to n-1.
- We can check for the duplicate indices using the “verify_integrity” parameter (by default = False). If the indices are duplicate and verify_integrity is set to “True”, it returns the “ValueError: Indexes have overlapping values”.
- It is possible to sort the columns if the columns of the existing DataFrame and another DataFrame are not aligned using the “sort” parameter by setting it to “True” (by default = False).
Example:
- Create an empty DataFrame which is labeled as “Campaign1” with two columns.
- Create the “Campaign2” DataFrame with two columns and four records.
- Concatenate both the DataFrames using the pandas.concat() function along the axis=0.
# Create empty DataFrame - Campaign2 with 2 columns
Campaign1=pandas.DataFrame(columns=['Campaign_Name','Location'])
print(Campaign1,"\n")
# Create DataFrame - Campaign1 with 2 columns and 4 records
Campaign2=pandas.DataFrame([['java camp','India'],['linux camp','USA'],['c/c++ camp','India'],['python camp','USA']],columns=['Campaign_Name','Location'])
print(Campaign2,"\n")
# Append Campaign2 to the Campaign1
Campaign1 = pandas.concat([Campaign2],axis=0,verify_integrity=True)
print(Campaign1)
Output:
Conclusion
We learned how to add rows to the empty Pandas DataFrame with four approaches. In all scenarios, it can be possible to add multiple rows at a time. Both pandas.concat() and pandas.DataFrame.append() are similar in terms of functionality and syntax. The pandas.DataFrame.loc[] property is utilized to add single/multiple rows to the DataFrame using the default row index.
Source: linuxhint.com