Pandas Append Not Working

Question:

datum = soup.findAll('a', {'class': 'result-title'})
for data in datum:
    print(data.text)
    print(data.get('href'))
    df = {'Title': data.text, 'Url': data.get('href')}
    houseitems.append(df, ignore_index=True)

What is wrong with my Code? Why when I asked for my houseitems, it gives me empty data.

Empty DataFrame

Columns: [Title, Url, Price]
Index: []
Asked By: user5235386

||

Answers:

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)
Answered By: jezrael

Try modify line in your code

houseitems.append(df, ignore_index=True)

as

houseitems=houseitems.append(df, ignore_index=True)
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.