in Education by
I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html 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 = dt.datetime.today().date() dates = [ base - dt.timedelta(days=x) for x in range(0,10) ] dates.sort() 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 Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
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) NewDF.head() 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.

Related questions

0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    How do I get the index column name in python pandas? Here's an example dataframe: Index Title Column 1 Apples 1 ... how to do this? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have a pandas data frame like: a b A 1 A 2 B 5 B 5 B 4 C 6 I want to group by the ... do something like this using pandas groupby? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have a dataframe as below itm Date Amount 67 420 2012-09-30 00:00:00 65211 68 421 2012-09-09 00 ... solutions would be appreciated. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    My data looks as follows: ID my_val db_val a X X a X X a Y X b X Y b Y Y b ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    def multiple_dfs(xyz_file, sheet, *args): row=2 writer = pd.ExcelWriter(xyz_file, engine='openpyxl') df = pd. ... help me over this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How to delete a column in a DataFrame, currently I am using: del df['column_1'] this is working fine, ... expected df.column_name ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    There is a DataFrame from pandas: import pandas as pd inp = [{'e2':20, 'e3':200}, {'e2':22,'e3':220}, { ... '] Can I do this in Pandas? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...