0

I need to declare empty dataframe in python in order to append it later in a loop. Below the line of the declaration:

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

It throws an error:

AssertionError: 5 columns passed, passed data had 0 columns

Why is it so? I tried to find out the solution, but I failed.

Aditi
  • 820
  • 11
  • 27
  • It seems you need `result_table = pd.DataFrame(columns = ["A", "B", "C", "D", "E"] )` – jezrael Jan 29 '18 at 12:48
  • 2
    The direct question has been answered... but the main question is "why do you want to append to it later"? – Jon Clements Jan 29 '18 at 12:49
  • Can you explain how do you want in loop create rows? I think it is not good idea, the best is fill lists and then once call DataFrame constructor. – jezrael Jan 29 '18 at 12:50
  • Possible duplicate of [Pandas create empty DataFrame with only column names](https://stackoverflow.com/questions/44513738/pandas-create-empty-dataframe-with-only-column-names) – Marcus V. Jan 29 '18 at 13:02

3 Answers3

0
import pandas as pd
df = pd.DataFrame(columns=['A','B','C','D','E'])

That's it!

piratefache
  • 1,308
  • 11
  • 17
0

Because you actually pass no data. Try this

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

if you then want to add data, use

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

I think it is better to create a list or tuples or lists and then call DataFrame only once:

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
Bjamse
  • 333
  • 5
  • 17
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252