In today’s data wrangling tutorial, we’ll learn a few methods that are readily available in Python in order to insert records into Pandas DataFrame.
We’ll start by loading a simple dataset to our Jupyter Notebook / Lab Namespace:
import pandas as pd
hr = pd.read_csv('hr.csv')
hr.head()
Create a new row as a list and insert it at bottom of the DataFrame
We’ll first use the loc indexer to pass a list containing the contents of the new row into the last position of the DataFrame. Note the usage of the the len(df)+1 parameter – which in our case, equals 5 to assign the contents of the list to the bottom of the DataFrame.
rec =['Python', 81, 88]
hr.loc[len(hr)+1] = rec
hr.head()
Add row at top of the DataFrame using Append
In this example, we’ll first convert a dictionary containing the new row content to a DataFrame and then use the DataFram.append() method to join it with our DataFrame:
# row contents as a dictionary
row_dict = {'language': ['C++'], 'avg_salary':[ 82], 'candidates': [89] }
# convert the dictionary to a DataFrame
hr2 = pd.DataFrame (row_dict)
# Append the two dataframes
hr3 = hr2.append(hr, ignore_index=True)
hr3.head(10)
Here’s our DataFrame:
Note: We can obviously create the new row and append it to the bottom of the DataFrame by calling the df.append() method on the original DataFrame:
hr3 = hr.append(hr2, ignore_index=True)
Create a new row from a dictionary and insert it to the DataFrame
In this example we’ll use the pd.concat() method. This time, we’ll inser the new row at the last position of our hr DataFrame.
# new row content
row_dict = {'language': ['Javascript'], 'avg_salary': [95], 'candidates': [78] }
row_df = pd.DataFrame (row_dict)
hr2 = pd.concat([hr, row_df], ignore_index=True)
hr2.head(10)
Here’s our DataFrame
Add two or more rows to a Python DataFrame
Adding multiple rows to the DataFrame can be executed as shown below. We’ll just need to pass the relevant data as a list or dictionary to the loc indexer, DataFrame.append() or pd.concat() methods.
Here’s a quick example:
# passing multiple rows
data_dict = {'language': ['Javascript', 'Java', 'C'], 'avg_salary': [95, 80,100], 'candidates': [78, 56, 45] }
#convert to DataFrame
hr1 = pd.DataFrame (data_dict)
#add the new rows
hr2 = pd.concat([hr, hr1], ignore_index=True)
hr2.head(10)
Add columns to a DataFrame
In previous posts, we covered the topic of inserting one or multiple new columns, both empty or containing data to a DataFrame. Feel free to look at them as needed 🙂