• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing Pandas Append Rows & Columns to Empty DataFrame

To append rows and columns to an empty DataFrame using the Pandas library in Python, you can use the append() method for rows and the bracket notation for columns. You can find out how to create an empty pandas DataFrame and append rows and columns to it by using DataFrame.append() method and DataFrame.loc[] property. In this article, I will explain how to append a row and column to empty DataFrame by several methods.

Related: In Pandas, you can append DataFrame using for loop.

1. Quick Examples to Append Empty DataFrame

If you are in a hurry below are some quick examples to append rows and columns to an empty DataFrame in pandas.


# Quick examples to append empty dataframe

# Example 1: Create a empty DataFrame
df = pd.DataFrame()

# Example 2: Append columns to an empty DataFrame
df['Courses'] = ['Spark', 'PySpark', 'Python']
df['Fee'] = [15000, 20000, 25000]
df['Duration'] = ['30days','35days','50days']

# Example 3: Append rows to empty DataFrame
df2 = df.append({'Courses' : 'Spark', 'Fee' : 15000, 'Discount' : '30days'}, ignore_index = True)

# Example 4: Create DataFrame with column name and indices 
# Using loc[] property
df = pd.DataFrame(columns = ['Courses', 'Fee', 'Duration'], 
                   index = ['1', '2', '3'])
df.loc['1'] = ['Courses',15000,'30days']

2. Append Columns to Empty DataFrame

First, let’s create an empty pandas DataFrame without any column names or indices and then append columns one by one to it.

In the below example, an empty DataFrame is created, and then three columns (‘Courses’, ‘Fee’, and ‘Duration’) are appended to it with corresponding values. The print statements are included to display the empty DataFrame and the DataFrame with the appended columns.


import pandas as pd

# Create an empty DataFrame
df = pd.DataFrame()
print("Empty DataFrame:")
print(df)

# Append columns to the empty DataFrame
df['Courses'] = ['Spark', 'PySpark', 'Python']
df['Fee'] = [15000, 20000, 25000]
df['Duration'] = ['30days', '35days', '50days']
print("\nDataFrame with appended columns:")
print(df)

Yields below output.

pandas append empty DataFrame

3. Append Rows to Empty DataFrame

pandas.DataFrame.append() function is used to add the rows of other DataFrame to the end of the given DataFrame and return a new DataFrame object.


# Append Rows to Empty DataFrame.
df2 = df.append({'Courses' : 'Spark', 'Fee' : 15000, 'Discount' : '30days'}, ignore_index = True)
print(df2)             

Yields below output.


# Output:
Empty DataFrame
Columns: [Courses, Fee, Duration]
Index: []
  Courses    Fee Duration Discount
0   Spark  15000      NaN   30days

4. By using loc[] to Append Row

You can find out how to create an empty DataFrame with column names and indices and then append rows one by one to it using DataFrame.loc[] property. The loc[] property is used to access a group of rows and columns by label(s) or a boolean array.


# Create DataFrame with Column name and indices 
# Using loc[] property
df = pd.DataFrame(columns = ['Courses', 'Fee', 'Duration'], 
                   index = ['1', '2', '3'])
df.loc['1'] = ['Courses',15000,'30days']
print(df)

Yields below output.


# Output:
Empty DataFrame
Columns: []
Index: []
   Courses    Fee Duration
1  Courses  15000   30days
2      NaN    NaN      NaN
3      NaN    NaN      NaN

5. Complete Example


# Create a empty DataFrame.
df = pd.DataFrame()

# Append columns to an empty DataFrame.
df['Courses'] = ['Spark', 'PySpark', 'Python']
df['Fee'] = [15000, 20000, 25000]
df['Duration'] = ['30days','35days','50days']
print(df)

# Append Rows to Empty DataFrame.
df2 = df.append({'Courses' : 'Spark', 'Fee' : 15000, 'Discount' : '30days'}, ignore_index = True)
print(df2)

# Create DataFrame with Column name and indices using loc[] property.
df = pd.DataFrame(columns = ['Courses', 'Fee', 'Duration'], 
                   index = ['1', '2', '3'])
df.loc['1'] = ['Courses',15000,'30days']
print(df)

Frequently Asked Questions on Append Rows & Columns to Empty DataFrame

How do I create an empty DataFrame in Pandas?

To create an empty DataFrame in Pandas, you can use the pd.DataFrame() constructor without any arguments. For example, to create an empty DataFrame named df. You can later append rows or columns to this DataFrame using various methods, such as append(), direct assignment, or concatenation.

How can I append a row to an empty DataFrame?

To append a row to an empty DataFrame in Pandas, you can use the append() method. For instance, a row with values for ‘Column1’ and ‘Column2’ is appended to the empty DataFrame using the append() method. The ignore_index=True argument ensures that the index is renumbered sequentially.

How do I append columns to an empty DataFrame?

To append columns to an empty DataFrame in Pandas, you can directly assign values to new columns. For example, two new columns, ‘Column1’ and ‘Column2’, are appended to the empty DataFrame, and values are assigned to them. You can continue to add more columns in a similar manner.

Can I append multiple rows or columns simultaneously?

You can append multiple rows or columns to a DataFrame simultaneously in Pandas. For instance, multiple rows are appended simultaneously by providing a list of dictionaries to the append method.

How do I append a DataFrame to another DataFrame?

To append one DataFrame to another DataFrame in Pandas, you can use the concat() function. For example, concat() is used to concatenate df1 and df2 along the rows (axis=0). The ignore_index=True argument ensures a continuous index in the resulting DataFrame.

Why should I use ignore_index=True when appending rows?

Setting ignore_index=True reindexes the resulting DataFrame, providing a continuous index without considering the original indices. This is useful when appending rows to ensure a consistent and unique index.

Conclusion

In this article, You have learned how to append rows, columns, and indices using DataFrame.append() and DataFrame.loc[] property with multiple examples.

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.