Back to Community
How to add entries to a dataframe?

What is the easiest / best way to add entries to a dataframe?

For example, when my algorithm makes a trade, I would like to record the sid and opening price in a custom dataframe, and then later append the price at which the position is exited.

Any help would be greatly appreciated.

5 responses
# Add a row to df  
from pandas import DataFrame as df  
mydf = df([1.1, 1.1, 1.1]).T  
arow2 = [2.2, 2.2, 2.2]  
mydf.loc[len(mydf)] = arow2  
print(mydf)

Thanks Dan, but I have not been able to get enlargement with .loc to work.
I get a "Runtime exception: KeyError: 1" on the "mydf.loc[len(mydf)] = arow2" line when using your code.
Any ideas or can you provide a working example/backtest?

Thanks again for the help,
Rob

You should post a screen dump.

Like this:

[email protected] /a/ks/tws/spy $ ipython  
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21)  
Type "copyright", "credits" or "license" for more information.

IPython 2.2.0 -- An enhanced Interactive Python.  
Anaconda is brought to you by Continuum Analytics.  
Please check out: http://continuum.io/thanks and https://binstar.org  
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.  
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from pandas import DataFrame as df

In [2]: mydf = df([1.1, 1.1, 1.1]).T

In [3]: arow2 = [2.2, 2.2, 2.2]

In [4]: mydf.loc[len(mydf)] = arow2

In [5]: print(mydf)  
     0    1    2  
0  1.1  1.1  1.1  
1  2.2  2.2  2.2

In [6]: 

Try this too:


# Add a row to df  
from pandas import DataFrame as df  
import numpy as np  
mydf = df([1.1, 1.1, 1.1]).T  
arow2 = [2.2, 2.2, 2.2]  
mynparray = mydf.values  
mynparray = np.vstack((mynparray,arow2))  
mydf = df(mynparray)

Thanks Dan. The numpy method works like a charm.

I could get .loc enlargement to work in iPython but not in the Quantopian IDE. I read that it is a relatively new feature and thought that might be the reason. Have you used/tested it in a quantopian algorithm?

Thanks again for the help and good luck with your trading.