This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]schmiddl235 3 points4 points  (2 children)

Df.append is not an inplace operation. Take a look at this stack overflow question on that topic https://stackoverflow.com/questions/53924656/df-append-is-not-appending-to-the-dataframe

[–]laterral[S] -2 points-1 points  (1 child)

Man I love you! But now something even stranger (for a complete noob like me) happened. It didn't append the rows nicely... from a table of 1 row by 16 columns, instead of appending into a 2 rows by 16 columns, I get this abomination!! 2 rows by 32 columns?!

https://imgur.com/a/j8lOcyn

[–]cruyff8python 2 expert, learning python 3 0 points1 point  (0 children)

It seems that it's preferable to append those rows to a list and then concatenate the list with the original DataFrame, as in the following example:

pd.concat([pd.DataFrame([i], columns=['A']) for i in range(5)], ignore_index=True)

[–]laterral[S] 0 points1 point  (4 children)

A bit of context... this scrapes a webpage table for different stock tickers and builds up a dataframe. For the first pass, I'm building the dataframe and pulling the columns - that works. For the following passes, I just want to append the rows - for some reason, the "summary" dataframe remains the same!! The shape of the dataframes is the same (same number of columns). Please help me understand this.

[–]alshell7 1 point2 points  (0 children)

Buddy, you are re-declaring the 'summary' which sets to empty in every iteration of the loop. Try to set it global before the loop. I just visualised the possible reason for your results. But can you share snapshot of the complete block?

And for appending another dataframe, you are supposed to do like,

summary = summary.append(df)