Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (19k points)

I'm starting from the pandas DataFrame docs here:

I'd like to iteratively fill the DataFrame with values in a time series kind of calculation. So basically, I'd like to initialize the DataFrame with columns A, B and timestamp rows, all 0 or all NaN.

I'd then add initial values and go over this data calculating the new row from the row before, say row[A][t] = row[A][t-1]+1 or so.

I'm currently using the code below, but I feel it's kind of ugly and there must be a way to do this with a DataFrame directly or just a better way in general. Note: I'm using Python 2.7.

import datetime as dt

import pandas as pd

import scipy as s

if __name__ == '__main__':

    base =

    dates = [ base - dt.timedelta(days=x) for x in range(0,10) ]


    valdict = {}

    symbols = ['A','B', 'C']

    for symb in symbols:

        valdict[symb] = pd.Series( s.zeros( len(dates)), dates )

    for thedate in dates:

        if thedate > dates[0]:

            for symb in valdict:

                valdict[symb][thedate] = 1+valdict[symb][thedate - dt.timedelta(days=1)]

    print valdict

1 Answer

0 votes
by (33.1k points)

You can simply use pandas.DataFrame() method:

To create an empty dataframe:

import pandas as pd 

Newdf = pd.DataFrame()

The above will create an empty dataframe of the Newdf variable.

To write the previous dataframe into a new one:

NewDF = NewDF.append(OldDF)


OldDF is the name of the previous dataframe, which you want to append in a new dataframe.

In the above code .head() method will print the first five rows of the dataframe.

Hope this answer helps.

Browse Categories