Categories
Python Answers

How to create a Python Pandas Dataframe by appending one row at a time?

Spread the love

To create a Python Pandas Dataframe by appending one row at a time, we use a for loop and add the entries to the loc dictionary.

For instance, we wtite

import pandas as pd
from numpy.random import randint

df = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
for i in range(5):
    df.loc[i] = ['name' + str(i)] + list(randint(10, size=2))

to create the df dataframe with pd.DataFrame with a few columns.

Then we add the rows by adding them to df.loc by using

df.loc[i] = ['name' + str(i)] + 

where list(randint(10, size=2)) is the new data.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *