user131983
user131983

Reputation: 3937

ValueError when trying to add row to dataframe

I am trying to add the row df[0] = [0,360, 0, 0, 0,2,3.9,100.01,100,0] to my dataframe df which is shown below but get the Error ValueError: Length of values does not match length of index. I have no idea why this is, as there are 10 values in this row and there are 10 columns.

          dt  months_to_maturity       asset_id            orig_iss_dt  \
1  2016-02-15                   6  (BINARY DATA)  1986-02-18 00:00:00.0   
2  2016-08-01                  12  (BINARY DATA)  1986-08-01 00:00:00.0   
3  2017-02-06                  18  (BINARY DATA)  1987-02-06 00:00:00.0 

              maturity_dt  pay_freq_cd  coupon  closing_price  FACE_VALUE  \
1   2016-02-15 00:00:00.0            2   9.250      105.40625         100   
2   2016-08-01 00:00:00.0            2   9.380      105.86200         100   
3   2017-02-06 00:00:00.0            2   8.450      111.42610         100

                            dt2  
1  1970-01-01 00:00:00.000000002  
2  1970-01-01 00:00:00.000000002  
3  1970-01-01 00:00:00.000000002 

Thank You

Upvotes: 0

Views: 73

Answers (1)

JoeCondron
JoeCondron

Reputation: 8906

Because

df[0] = ...

Is the syntax for adding a column, not a row, and so should the array on the right should be of the same length as the columns, i.e. the number of rows (or a scalar). If you want add a row do

df.loc[0] = <values>

I guess part of the confusion comes from the fact that this is sort of the opposite to numpy syntax, in the sense that a[0] = <values> assigns values to the zeroth row rather than column.

Upvotes: 2

Related Questions