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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.