Python pd.DataFrame: 解决 ‘ValueError: Shape of passed values is (6, 1), indices imply (6, 6)’
问题:
在使用Python pandas 创建DataFrame时,出现ValueError: Shape of passed values is (6, 1), indices imply (6, 6)
。
原因:
输入的list为: test_list = [1,2,3,4,5,6]
。
创建DataFrame:
test_df = pd.DataFrame(test_list, columns = ['A', 'B', 'C', 'D', 'E', 'F'])
反馈错误: ValueError: Shape of passed values is (6, 1), indices imply (6, 6)
∴
\therefore
∴ 可以从错误中得知,输入的list为长度6的list(6行rows),而DataFrame需要的是6列(columns)的list。因此,需要将test_list
改为(1*6)的list就可以了。
具体改法:
test_list = np.array(test_list).reshape(1,6)
test_list = [[1,2,3,4,5,6]]