【问题标题】:assigning a list of tokens as a row to dataframe将令牌列表作为一行分配给数据框
【发布时间】:2016-05-11 16:22:14
【问题描述】:

我正在尝试创建一个数据框,其中第一列是令牌列表,并且可以添加其他信息列。但是,pandas 不允许将标记列表添加为一列。 所以代码如下所示

  array1 = ['two', 'sample', 'statistical', 'inferences', 'includes']
  array2 = ['references', 'please', 'see', 'next', 'page', 'the','material', 'of', 'these']
  array3 = ['time', 'student', 'interest', 'and', 'lecturer', 'preference', 'other', 'topics']


 ## initialise list 
 list = []
list.append(array1)
list.append(array2)
list.append(array3)

## create dataFrame 
numberOfRows = len(list)
df = pd.DataFrame(index=np.arange(0, numberOfRows), columns = ('data',    'diversity'))

df.iloc[0] = list[0]   

错误信息显示

ValueError: cannot copy sequence with size 6 to array axis with dimension 2

任何关于如何更好地创建数据框和更新列的见解将不胜感激。 谢谢

【问题讨论】:

  • 你是df = pd.DataFrame({'data': list}); df['diversity'] = '?'吗?其实不要用list这样的保留字来表示变量!

标签: python-3.x pandas dataframe


【解决方案1】:

好的,所以答案很简单,为了繁荣而发布它。 将列表添加为行时,我需要包含列名和位置.. 所以代码如下所示。

   df.data[0] = array1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2021-01-14
    • 2020-05-23
    相关资源
    最近更新 更多