I understand that pandas is designed to load fully populated DataFrame
but I need to create an empty DataFrame then add rows, one by one.
What is the best way to do this ?
I successfully created an empty DataFrame with :
res = DataFrame(columns=('lib', 'qty1', 'qty2'))
Then I can add a new row and fill a field with :
res = res.set_value(len(res), 'qty1', 10.0)
It works but seems very odd :-/ (it fails for adding string value)
How can I add a new row to my DataFrame (with different columns type) ?
Make it simple. By taking list as input which will be appended as row in data-frame:-
Here is the way to add/append a row in pandas DataFrame
It can be used to insert/append a row in empty or populated pandas DataFrame
You can append a single row as a dictionary using the
ignore_index
option.You could use
pandas.concat()
orDataFrame.append()
. For details and examples, see Merge, join, and concatenate.In case you can get all data for the data frame upfront, there is a much faster approach than appending to a data frame:
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.