Create A Pandas Dataframe By Appending One Row At A Time

To create a Pandas DataFrame by appending one row at a time, you can follow the steps below:

  1. Import the Pandas library:
import pandas as pd
  1. Create an empty DataFrame:
df = pd.DataFrame(columns=['Column1', 'Column2', 'Column3'])
  1. Loop over your data and append rows to the DataFrame:
for i in range(len(data)):
    row = data[i]  # Suppose data is a list of values for each column
    df.loc[i] = row
  1. The DataFrame will be created and populated with one row at a time. You can view the DataFrame by printing it:

Here is an example where we create a DataFrame with three columns (‘Name’, ‘Age’, ‘Country’) and append three rows to it:

import pandas as pd

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

# Data to be added row-by-row
data = [
    ['John', 25, 'USA'],
    ['Emily', 30, 'Canada'],
    ['Michael', 35, 'UK']

# Append one row at a time
for i in range(len(data)):
    row = data[i]
    df.loc[i] = row

# Print the DataFrame


      Name Age Country
0     John  25     USA
1    Emily  30  Canada
2  Michael  35      UK

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!