从列表类型创建数据帧错误:“int”类型的对象没有len()

2024-04-25 22:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个列表中创建一个数据帧,该列表的每一行都有不同的长度。在

列表的一个示例如下所示(这就是我想要的)

[(dwstweets gop, broadened, base people), 1]
[(bushs campaign video, features, kat), 2]
[3]
[4]
[5]
[(president obama, wants, york), 6]
[(jeb bush, talked, enforcement), (lets, see, plan), 7]

我使用try和append The list with each row来创建数据帧的代码是:

^{pr2}$

但是我得到了一个错误:

TypeError: object of type 'int' has no len()

我看到有几个问题与这个错误,但据我所见,他们不是由同一件事引起的。在

我该如何创建一个索引上有7列唯一的数据帧?(我知道其中至少有3列和除索引外的所有列,其中许多列都是空的)

谢谢。在


Tags: 数据示例列表basevideo错误peoplefeatures
2条回答

我建议先按append按不带[index]的元组创建元组列表,然后调用DataFrame构造函数,如下所示:

count = 0
L = []
df2 = pd.DataFrame();
for index, row in df1.iterrows():
  doc = nlp(unicode(row))
  text_ext = textacy.extract.subject_verb_object_triples(doc)
  #remove join index 
  mylist = list(text_ext)
  count+=1;
  #append to list
  L.append(mylist)

df2 = pd.DataFrame(L, index=df1.index)
print (df2)
                                         0                  1
1  (dwstweets gop, broadened, base people)               None
2    (bushs campaign video, features, kat)               None
3                                     None               None
4                                     None               None
5                                     None               None
6           (president obama, wants, york)               None
7          (jeb bush, talked, enforcement)  (lets, see, plan)

我相信错误可能在代码中的for循环行中:

for index, row in df1.iterrows():

在DataFrame.i错误()返回一个迭代器对象,该对象至少在本例中不能用于定义for循环。在

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html

相关问题 更多 >