0

So I am getting bunch of arrays in streaming fashion that I want to append to a dataframe. I am trying to do it as follows

df = pd.DataFrame(columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y'])

outputs = get_outputs()

for xp, yp in zip(outputs, labels):
     ex = np.append(xp, yp)
     print(ex)
     print(ex.shape)
     #trying here to create row-dataframe
     exdf = pd.DataFrame(ex, columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y']) 
     #want to append the row to the main dataframe
     df.append(exdf) 

I get output and error such

[  4.49039745  -9.63315201   7.70181465 -15.19582367  12.6580925
  -1.17788887  -5.21339655   2.6664052    2.96283174   1.22973883
   6.        ]
(11,)
...
ValueError: Shape of passed values is (11, 1), indices imply (11, 11)

How am I supposed to do it?

EDIT: after incorporating changing in answer

exdf = pd.DataFrame([ex], columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y'])
print(exdf)
df.append(exdf)
print(df[:1])

I receive empty dataframe

Out
          1         2         3          4  ...         8         9        10    y
0  4.490397 -9.633152  7.701815 -15.195824  ...  2.666405  2.962832  1.229739  6.0

[1 rows x 11 columns]
Empty DataFrame
Columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, y]
Index: []

1 Answer 1

1

Adding [] in your for loop

 for xp, yp in zip(outputs, labels):
 ex = np.append(xp, yp)
 print(ex)
 print(ex.shape)
 #trying here to create row-dataframe
 exdf = pd.DataFrame([ex], columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y']) 
 #want to append the row to the main dataframe
 df=df.append(exdf) 
Sign up to request clarification or add additional context in comments.

3 Comments

I updated my post. Now for some reason dataframe is empty
@YohanRoth try pd.DataFrame(ex[:,None], columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y'])
@YohanRoth also , you need assign it back df=df.append(..), append in pandas is different from append for list in python , we need to assign it back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.