pythonpandasdataframe

pandas dataframe, how to add new row efficiently


I would like to know how to add a new row efficiently to the dataframe.

Assuming I have a empty dataframe

"A" "B"

columns = ['A','B']

user_list = pd.DataFrame(columns=columns)

I want to add one row like {A=3, B=4} to the dataframe, how to do that in most efficient way?


Solution

  • columns = ['A', 'B']
    user_list = pd.DataFrame(np.zeros((1000, 2)) + np.nan, columns=columns)
    user_list.iloc[0] = [3, 4]
    user_list.iloc[1] = [4, 5]
    

    Pandas doesn't have built-in resizing, but it will ignore nan's pretty well. You'll have to manage your own resizing, though :/