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) ?
For the sake of Pythonic way, here add my answer:
This will take care of adding an item to an empty DataFrame. The issue is that df.index.max() == nan for the first index:
Another way to do it (probably not very performant):
You can also enhance the DataFrame class like this:
For efficient appending see How to add an extra row to a pandas dataframe and Setting With Enlargement.
Add rows through
loc/ix
on non existing key index data. e.g. :Or:
Example at @Nasser's answer: