0
df = ({'a':[0,1,4],'b' : [10,2,5]})
y = [5,6]                

for i in y:
    x = df._append([{df.columns[0]: i}], ignore_index = True)
print(x)

Result is :

{'a':[0,1,4,6],
 'b' : [10,2,5]}

Result Should be:

{'a':[0,1,4,5,6],
 'b' : [10,2,5]} 

Any help please?

not_speshal
  • 22,093
  • 2
  • 15
  • 30

2 Answers2

2

Use pd.concat:

pd.concat([df, pd.DataFrame({df.columns[0]:y})])

Output:

    A   B
0   0   10.0
1   1   2.0
2   4   5.0
0   5   NaN
1   6   NaN
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

Why on earth would you use _append? This is an undocumented, private pandas method.

append was removed from pandas API because it was used so badly.

You should never append to a DataFrame iteratively.

The correct approach to perform this operation is concat:

x = pd.concat([df, pd.DataFrame({'A': y})])

Output:

   A     B
0  0  10.0
1  1   2.0
2  4   5.0
0  5   NaN
1  6   NaN
mozway
  • 194,879
  • 13
  • 39
  • 75