# Create an empty Python Pandas DataFrame and add rows

**URL:** https://sentry.io/answers/create-an-empty-python-pandas-dataframe-and-add-rows/

---

## The Problem

I would like to create an empty Python Pandas DataFrame and add rows to it one by one. How can I achieve this?

## The Solution

While it is possible to add rows to a DataFrame after it has been created, this approach has several downsides compared to the standard practice of creating a new DataFrame from a list or dictionary. It is slower and more memory intensive, the datatypes for elements in new rows will not be automatically inferred, and numeric labels may not behave as desired. Per the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.concat.html):

> It is not recommended to build DataFrames by adding single rows in a for loop.

If we want to build up data for a DataFrame iteratively, we should work with either dictionaries or lists until we're ready to create the final DataFrame. For example:

```python
import pandas

# Make a 5x5 list of lists
data = []
for x in range(5):
    data.append([])
    for y in range(5):
        data[x].append(y)

# Create a DataFrame from the list of lists
df = pandas.DataFrame(data)
print(df)

# will print
#    0  1  2  3  4
# 0  0  1  2  3  4
# 1  0  1  2  3  4
# 2  0  1  2  3  4
# 3  0  1  2  3  4
# 4  0  1  2  3  4
```

In some instances, this may be insufficient for our needs. Older versions of Pandas provided a `DataFrame.append` method, but this was removed in pandas 2.0 in favor of [`pandas.concat`](https://pandas.pydata.org/docs/reference/api/pandas.concat.html). We can use this latter method to add rows to an existing DataFrame:

```python
import pandas

# Make a 5x5 list of lists
data = []
for x in range(5):
    data.append([])
    for y in range(5):
        data[x].append(y)

# Create a DataFrame from the list of lists
df = pandas.DataFrame(data)

# Create a new row and add it to the DataFrame
new_row = pandas.DataFrame([[0, 1, 2, 3, 4]])
df = pandas.concat([df, new_row], ignore_index=True)
print(df)

# will print
#    0  1  2  3  4
# 0  0  1  2  3  4
# 1  0  1  2  3  4
# 2  0  1  2  3  4
# 3  0  1  2  3  4
# 4  0  1  2  3  4
# 5  0  1  2  3  4
```

By passing `ignore_index=True`, the resulting DataFrame is given a new sequential integer index, so the new row is automatically labeled `5`.

---

*Source: [sentry.io/answers/create-an-empty-python-pandas-dataframe-and-add-rows/](https://sentry.io/answers/create-an-empty-python-pandas-dataframe-and-add-rows/)*
