【发布时间】: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