[英]Pandas: need to add a new column from a numpy array, but the length is longer than the dataframe's length
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 : 现在我想从numpy数组中添加一个列:
`[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
我试过
df_csv_exposure['time1_low'] = time1_low
但得到了消息错误: 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 基本上,数组
time1_low
只比数据帧的其余部分长一行
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.
在这种情况下,最后一行将需要具有其他列的Nonetype值。 You should simply add a "None" row to your original dataset, and then join the column to this new dataframe with identical length.
您只需在原始数据集中添加“无”行,然后将列连接到具有相同长度的新数据帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.