Create a Pandas Dataframe by appending one row at a time

Here is an example of creating a Pandas DataFrame by appending one row at a time:

import pandas as pd

# Create an empty DataFrame
df = pd.DataFrame(columns=['Name', 'Age'])

# Append rows to the DataFrame
df = df.append({'Name': 'John', 'Age': 30}, ignore_index=True)
df = df.append({'Name': 'Mike', 'Age': 25}, ignore_index=True)
df = df.append({'Name': 'Sarah', 'Age': 35}, ignore_index=True)

# Print the DataFrame
print(df)

This will output:

Name Age
0  John  30
1  Mike  25
2 Sarah  35

Watch a course Python - The Practical Guide

Alternatively, you can use the loc accessor of DataFrame to add the row one by one,

df = pd.DataFrame(columns=['Name', 'Age'])
df.loc[0] = ['John', 30]
df.loc[1] = ['Mike', 25]
df.loc[2] = ['Sarah', 35]

In both cases, the ignore_index parameter set to True when appending, will reset the index of the DataFrame to a simple sequence starting from 0.