0

My dataframe looks like this:

      time1_high          time2_high  price1_high  price2_high
0 18008 days 00:32:44 18008 days 00:35:40     0.001850     0.001850
1 18008 days 01:16:19 18008 days 01:17:24     0.001845     0.001845
2 18008 days 02:28:50 18008 days 02:29:28     0.001842     0.001842
3 18008 days 02:30:37 18008 days 02:31:51     0.001842     0.001842
4 18008 days 04:48:37 18008 days 04:50:02     0.001837     0.001838
5 18008 days 09:57:19 18008 days 10:00:17     0.001841     0.001842
6 18008 days 10:19:30 18008 days 10:21:07     0.001845     0.001845

Now I would like to add a column from a numpy array :

`[Timedelta('18008 days 00:21:37') Timedelta('18008 days 00:46:54')
 Timedelta('18008 days 01:14:40') Timedelta('18008 days 04:07:29')
 Timedelta('18008 days 04:19:39') Timedelta('18008 days 07:05:45')
 Timedelta('18008 days 07:43:15') Timedelta('18008 days 09:22:00')]

but the length is longer than the dataframe, how to proceed?

I tried df_csv_exposure['time1_low'] = time1_low but got as message error: Length of values does not match length of index

What I would like is a dataframe looking like that:

         time1_high          time2_high  price1_high  price2_high        time1_low
    0 18008 days 00:32:44 18008 days 00:35:40     0.001850     0.001850  `'18008 days 00:21:37
    1 18008 days 01:16:19 18008 days 01:17:24     0.001845     0.001845 18008 days 00:46:54'
    2 18008 days 02:28:50 18008 days 02:29:28     0.001842     0.001842 18008 days 01:14:40
    3 18008 days 02:30:37 18008 days 02:31:51     0.001842     0.001842 18008 days 04:07:29
    4 18008 days 04:48:37 18008 days 04:50:02     0.001837     0.001838 18008 days 04:19:39
    5 18008 days 09:57:19 18008 days 10:00:17     0.001841     0.001842 18008 days 07:05:45
    6 18008 days 10:19:30 18008 days 10:21:07     0.001845     0.001845 18008 days 09:22:00
                                                                  18008 days 07:43:15

Basically the array time1_low is just one row longer than the rest of the dataframe

Viktor.w
  • 1,787
  • 2
  • 20
  • 46
  • 1
    possible duplicate https://stackoverflow.com/questions/42382263/valueerror-length-of-values-does-not-match-length-of-index-pandas-dataframe-u/42382321 – VnC May 13 '19 at 15:09
  • 1
    Do you need to align the values? Or you just want a simple concatenation? Can you show your expected output? – Quang Hoang May 13 '19 at 15:10
  • 2
    I don't know how to proceed for the same reason you are getting the error. How do I know what element of the array goes to which row of the dataframe? – piRSquared May 13 '19 at 15:10
  • I tried this approach but did not work – Viktor.w May 13 '19 at 15:10
  • @piRSquared, the numpy array that I want to add into a new column in the dataframe is juste longer (8 values) than the current columns which have only 7 values.. – Viktor.w May 13 '19 at 15:37
  • you want to fill in from the beginning as long as you have stuff to fill or stuff to fill with? `df['time1_low'] = time1_low[:len(df)]` – piRSquared May 13 '19 at 15:39
  • The length of data that I want to put in the new column has more values than the length of data in the current columns. So I get an error. I apply your method it gives the same error message @piRSquared – Viktor.w May 13 '19 at 15:50

1 Answers1

0

As far as I'm aware, dataframes just can't be uneven. In this situation, the final row will need to have Nonetype values for other columns. You should simply add a "None" row to your original dataset, and then join the column to this new dataframe with identical length.

Jim Eisenberg
  • 1,490
  • 1
  • 9
  • 17