0

I am trying to add another row to my data Frame

When I use df["new_row"] = [5, True, "joe", 20] , I get the error

ValueError: Length of values (4) does not match length of index (11)

but if I use df.loc["new_row"] = [5, True, "joe", 20], I can succesfully add a new row.

quant
  • 17
  • 5
  • Does this answer your question? [What is the difference between using loc and using just square brackets to filter for columns in Pandas/Python?](https://stackoverflow.com/questions/48409128/what-is-the-difference-between-using-loc-and-using-just-square-brackets-to-filte) – Mark Rotteveel Nov 21 '22 at 08:54

1 Answers1

0

To answer your question about the difference, in pandas, df["colname"] is used to access a column of a given data frame.

loc[r,c] is used to access specific cells within that data frame in the order of row and column. So, if you use df.loc[r], it will access the entire row.

In your case,df.loc['new_row'], creates a new row, at which you are inserting [5, True, "joe", 20]