Python Forum

Full Version: Append to dataframe with for loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to loop through a list(y) and output by appending a row for each item in y to a dataframe.
y=[datetime.datetime(2017, 3, 29), datetime.datetime(2017, 3, 30), datetime.datetime(2017, 3, 31)]

Output:
Index              Mean    Last 2017-03-29     1.5      .76 2017-03-30      2.3      1 2017-03-31     1.2       .4
Here is the first and last part of the code I have:


df5=pd.DataFrame(columns=['Mean','Last'],index=index)
for item0 in y:
          
..............
..............
             df=df.rename(columns = {0:'Mean'})
             df4=pd.concat([df, df3], axis=1)
    
             df5.append(df4)
             print (df5)
It only puts one row into the dataframe like:
Output:
Index              Mean    Last 2017-03-29     1.5      .76
Some sample input, the output you're getting, and the output you're hoping for would be a nice place to start.
(Apr-27-2017, 06:48 PM)nilamo Wrote: [ -> ]Some sample input, the output you're getting, and the output you're hoping for would be a nice place to start.

Yeah sorry I hit tab and posted it before I was ready. But now its ready.