Python Forum
Append to dataframe with for loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Append to dataframe with for loop (/thread-3076.html)



Append to dataframe with for loop - nocoffeenoworkee - Apr-27-2017

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



RE: Append to dataframe with for loop - nilamo - Apr-27-2017

Some sample input, the output you're getting, and the output you're hoping for would be a nice place to start.


RE: Append to dataframe with for loop - nocoffeenoworkee - Apr-27-2017

(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.