Pandas Append Rows & Columns to Empty DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 11, 2023

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.


# Below are some quick examples.

# 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']

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

# 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)

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.


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

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

Yields below output.


# Output:
Empty DataFrame
Columns: []
Index: []
   Courses    Fee Duration
0    Spark  15000   30days
1  PySpark  20000   35days
2   Python  25000   50days

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 of Append Rows & Columns to Empty DataFrame


# 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)

Conclusion

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

Leave a Reply

You are currently viewing Pandas Append Rows & Columns to Empty DataFrame