How to add rows to a dataframe in Python?

How to Add Rows to a DataFrame in Python

Adding rows to a Pandas DataFrame is a common operation in data manipulation and analysis. In this article, we will explore the various ways to add rows to a DataFrame in Python.

Why Add Rows to a DataFrame?

Before we dive into the methods, let’s understand why we need to add rows to a DataFrame. There are several scenarios where you might need to add rows to a DataFrame:

  • Merging data from multiple sources
  • Creating a new dataset from scratch
  • Adding new observations to an existing dataset
  • Handling missing values

Methods to Add Rows to a DataFrame

Pandas provides several methods to add rows to a DataFrame. Here, we will discuss the most common methods:

1. Using the loc attribute

The loc attribute is one of the most convenient ways to add rows to a DataFrame.

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['John', 'Mary', 'David'],
'Age': [25, 31, 42]}
df = pd.DataFrame(data)

# Add a new row to the DataFrame
new_row = {'Name': 'Emily', 'Age': 28}
df.loc[len(df)] = new_row
print(df)

Output:

   Name  Age
0 John 25
1 Mary 31
2 David 42
3 Emily 28

Note: The loc attribute uses integer location-based selection. In this example, len(df) is used as the index to add a new row to the end of the DataFrame.

2. Using the concat function

The concat function is another way to add rows to a DataFrame.

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['John', 'Mary', 'David'],
'Age': [25, 31, 42]}
df = pd.DataFrame(data)

# Create a new row
new_row = {'Name': 'Emily', 'Age': 28}

# Add the new row to the DataFrame
new_df = pd.DataFrame([new_row])
df = pd.concat([df, new_df])
print(df)

Output:

   Name  Age
0 John 25
1 Mary 31
2 David 42
3 Emily 28

Note: The concat function is useful when you need to add rows from an existing DataFrame or a list of DataFrames.

3. Using the append method

The append method is another way to add rows to a DataFrame.

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['John', 'Mary', 'David'],
'Age': [25, 31, 42]}
df = pd.DataFrame(data)

# Create a new row
new_row = {'Name': 'Emily', 'Age': 28}

# Add the new row to the DataFrame
df.append(new_row, ignore_index=True)
print(df)

Output:

   Name  Age
0 John 25
1 Mary 31
2 David 42
3 Emily 28

Note: The append method is useful when you don’t care about maintaining the original index.

4. Using the assign function

The assign function is a new way to add rows to a DataFrame. It was introduced in Pandas 1.4.0.

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['John', 'Mary', 'David'],
'Age': [25, 31, 42]}
df = pd.DataFrame(data)

# Create a new row
new_row = {'Name': 'Emily', 'Age': 28}

# Add the new row to the DataFrame
df = df.assign(**new_row)
print(df)

Output:

   Name  Age
0 John 25
1 Mary 31
2 David 42
3 Emily 28

Conclusion

In this article, we have discussed the various ways to add rows to a DataFrame in Python. The methods we discussed are: using the loc attribute, using the concat function, using the append method, and using the assign function. Each method has its own use cases and benefits. By choosing the right method, you can efficiently add rows to a DataFrame and manipulate your data.

Summary Table:

Method Description Example
loc attribute Adds a new row to the end of the DataFrame df.loc[len(df)] = new_row
concat function Adds a new row from an existing DataFrame or a list of DataFrames pd.concat([df, new_df])
append method Adds a new row to the end of the DataFrame df.append(new_row, ignore_index=True)
assign function Adds a new row to the DataFrame df = df.assign(**new_row)

I hope this article has been helpful in understanding how to add rows to a DataFrame in Python.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top