0

I have a portion of my dataframe here:

days = [1, 2, 3, 4, 5]

time = [2, 4, 2, 4, 2, 4, 2, 4, 2]

df1 = pd.DataFrame(days)

df2 = pd.Series(time)
df2 = df2.transpose()

df3 = df1*df2
df4 = df1.dot(df2.to_frame().T)
df4 = 
    0   1   2   3   4   5   6   7   8
0   2   4   2   4   2   4   2   4   2
1   4   8   4   8   4   8   4   8   4
2   6   12  6   12  6   12  6   12  6
3   8   16  8   16  8   16  8   16  8
4   10  20  10  20  10  20  10  20  10



I have an if loop that creates a single row dataframe which looks like:

df_new = 
    0   1   2   3   4   5   6   7   8
0   2   4   2   4   2   4   2   4   2

I need to be able to loop through and add this row to the end of the larger dataframe a handful of times so the end result looks like this:

df_final = 
    0   1   2   3   4   5   6   7   8
0   2   4   2   4   2   4   2   4   2
1   4   8   4   8   4   8   4   8   4
2   6   12  6   12  6   12  6   12  6
3   8   16  8   16  8   16  8   16  8
4   10  20  10  20  10  20  10  20  10
5   2   4   2   4   2   4   2   4   2
6   5   6   7   8   9   8   7   6   5

I have tried to either append or concact the new dataframe to the existing one, but I receive errors both ways. Either indexing errors or a few looping issues. I think I need either a better understanding of why the row cannot be added to the end of the dataframe or an idea of a work around. The loop has 25 iterations where I only added two, but the idea is the same, I will get a new row in the form of a single row dataframe and I need to add that data from the single row dataframe without the column headers to the final dataframe. I am willing to update my question as soon as I get a better idea of how this can work, it does not seem like a difficult task, but I am sure I am asking the wrong thing.

JakeP
  • 77
  • 6
  • Does this answer your question? [Appending pandas dataframes generated in a for loop](https://stackoverflow.com/questions/28669482/appending-pandas-dataframes-generated-in-a-for-loop) – Paul Brennan Aug 16 '22 at 18:55
  • So when I do that and try the concat step, the list overwrites each iteration so only that row that is created on that iteration is added and then replaced each time through. – JakeP Aug 16 '22 at 19:00
  • 1
    Did you set up a new empty data structure to take the value of the concat? the method in the answer to the question works well in my case – Paul Brennan Aug 16 '22 at 19:08

0 Answers0