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.
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
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.
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.
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.
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.
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.
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.
Related Articles
- Pandas Find Unique Values From Columns
- Pandas append() Usage by Examples
- How to Append Pandas Series?
- Difference between two DataFrames
- Append Pandas DataFrames Using for Loop
- How to Append Row to pandas DataFrame
- Pandas compare two DataFrames row by row
- Add Constant Column to Pandas DataFrame
- Rename Index Values of Pandas DataFrame
- How to Rename Columns With List in Pandas
- How to Print Pandas DataFrame without Index
- Retrieve Number of Columns From Pandas DataFrame