user5739619 user5739619 - 1 year ago 148
Python Question

Pandas pd.DataFrame converts to Tuples instead of Dataframe

I first use BeautifulSoup:

mydivs = soup.findAll('div', {"class": "content"})


so that each
mydiv
in
mydivs
looks like this for example:

<div class="content">A number of hats by me <br/><br/>three now though ... </div>


I then want to store each of the text blocks in each
div
as rows in a dataframe. I want the dataframe to look something like:

index posts
0 <div class="content">A number of <br/><br/>three ... </div>
1 <div class="content">Stack ... <br/><br/>overflow ... </div>
...


This is the code I tried

A=[]
indices=[]
j=0

for div in mydivs:
A.append(div)
indices.append(j)
j+=1

DF = pd.DataFrame({'index': indices, "posts": A})


When I then print out the
shape
I get

print DF.shape()
TypeError: 'tuple' object is not callable


However, I want
DF
to be a dataframe, not a
tuple
. How can I fix this?

Answer Source

shape is an attribute of DF. That attribute is a tuple. You are trying to call it with the () which is throwing the error. If you want the shape just do DF.shape

print DF.shape

not

print DF.shape()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download