我正在从文本文件中构建已清理数据的数据框。我的预期数据框是 11 列和 2 行(目前)。
TestRow1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
columns_headers = [A, B, C, D, E, F, G, H, I, J, K]
我认为这会起作用:
new_df = pd.DataFrame(data =TestRow1, headers = columns_headers)
它没有。相反,您会收到索引错误:
error ValueError: Shape of passed values is (11, 1), indices imply (11, 11)
但是,当我通过这个时:
NewDF = pd.DataFrame(data = [TestRow1], columns = columns_headers)
它确实有效。但为什么?如果检查'TestRow1'和'[TestRow1]'的类型,你会发现它们都是:
<class 'list'>
那么这里发生了什么?