Create a Pandas Dataframe by appending one row at a time Create a Pandas Dataframe by appending one row at a time python python

Create a Pandas Dataframe by appending one row at a time


You can use df.loc[i], where the row with index i will be what you specify it to be in the dataframe.

>>> 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))>>> df     lib qty1 qty20  name0    3    31  name1    2    42  name2    2    83  name3    2    14  name4    9    6


In case you can get all data for the data frame upfront, there is a much faster approach than appending to a data frame:

  1. Create a list of dictionaries in which each dictionary corresponds to an input data row.
  2. Create a data frame from this list.

I had a similar task for which appending to a data frame row by row took 30 min, and creating a data frame from a list of dictionaries completed within seconds.

rows_list = []for row in input_rows:        dict1 = {}        # get input row in dictionary format        # key = col_name        dict1.update(blah..)         rows_list.append(dict1)df = pd.DataFrame(rows_list)               


You could use pandas.concat() or DataFrame.append(). For details and examples, see Merge, join, and concatenate.