Python Pandas声明只有列的空DataFrame

时间:2018-01-29 12:46:15

标签: python pandas dataframe

我需要在python中声明空数据帧,以便稍后在循环中附加它。在声明的下面:

 result_table = pd.DataFrame([[], [], [], [], []], columns = ["A", "B", "C", "D", "E"])

它会抛出错误:

  

AssertionError:传递了5列,传递的数据有0列

为什么会这样?我试图找出解决方案,但我失败了。

3 个答案:

答案 0 :(得分:0)

@staticmethod
def initiate_pdf_processing(ct_doc, pt_doc, feature, startAndEndKeyList):
    logging.info("testing logger")
    ...

就是这样!

答案 1 :(得分:0)

因为你实际上没有传递任何数据。试试这个

result_frame = pd.DataFrame(columns=['a', 'b', 'c', 'd', 'e'])

如果您想添加数据,请使用

result_frame.loc[len(result_frame)] = [1, 2, 3, 4, 5]

答案 2 :(得分:0)

我认为最好创建listtuplelist,然后只调用DataFrame一次:

L = []
for i in range(3):
    #some random data  
    a = 1
    b = i + 2
    c = i - b
    d = i
    e = 10
    L.append((a, b, c, d, e))

print (L)
[(1, 2, -2, 0, 10), (1, 3, -2, 1, 10), (1, 4, -2, 2, 10)]

result_table = pd.DataFrame(L, columns = ["A", "B", "C", "D", "E"])
print (result_table) 
   A  B  C  D   E
0  1  2 -2  0  10
1  1  3 -2  1  10
2  1  4 -2  2  10