Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Append Not Working

Tags:

python

pandas

People also ask

How do I use append in pandas?

Series append syntax The syntax for using append on a Series is very similar to the dataframe syntax. You type the name of the first Series, and then . append() to call the method. Then inside the parenthesis, you type the name of the second Series, which you want to append to the end of the first.

How do I append to a column in pandas?

In pandas you can add/append a new column to the existing DataFrame using DataFrame. insert() method, this method updates the existing DataFrame with a new column. DataFrame. assign() is also used to insert a new column however, this method returns a new Dataframe after adding a new column.


Try modify line in your code

houseitems.append(df, ignore_index=True)

as

houseitems=houseitems.append(df, ignore_index=True)

Problem is you need assign back appended DataFrame, because pandas DataFrame.append NOT working inplace like pure python append.

It seem you want append to list, so parameter ignore_index=True is not necessary:

Loop solution:

houseitems = []
for data in datum:
    print(data.text)
    print(data.get('href'))
    df = {'Title': data.text, 'Url': data.get('href')}
    houseitems.append(df)

Or list comprehension solution:

houseitems = [{'Title': data.text, 'Url': data.get('href')} for data in datum]

And then create DataFrame:

df1 = pd.DataFrame(houseitems)