2016-09-05 62 views
2

我第一次使用BeautifulSoup:熊猫pd.DataFrame转换成元组,而不是数据帧

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

使得mydivs每个mydiv看起来像这样的例子:

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

我再要存储每个每个div中的文本块作为数据帧中的行。我想数据框看起来是这样的:

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

这是我尝试

A=[] 
indices=[] 
j=0 

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

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

的代码时,我然后打印出shape我得到

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

不过,我想DF成为数据帧,而不是tuple。我怎样才能解决这个问题?

回答

3

形状是DF的属性。该属性是tuple。你正试图用()来调用它,这是抛出错误。如果你想要的形状只是做DF.shape

print DF.shape 

print DF.shape()