简体   繁体   English

Pandas:需要从numpy数组中添加一个新列,但是长度比数据帧的长度长

[英]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.

相关问题 Python-将numpy数组作为列添加到具有不同长度的pandas数据帧 - Python - add a numpy array as column to a pandas dataframe with different length 将较短长度的Numpy数组连接到Pandas数据框 - Joining Shorter Length Numpy Array to Pandas Dataframe 使用长度小于pandas数据框的numpy数组填充pandas数据框 - Using a numpy array of length less than pandas dataframe to fill in pandas dataframe 从pandas数据帧中删除行,其中句子的句子长度超过某个字长 - Remove the rows from pandas dataframe, that has sentences longer than certain word length Pandas/Numpy - 如何在与索引长度匹配的数据帧列中添加重复出现的整数 - Pandas/Numpy - how to add reoccurring integers in a dataframe column that matches the length of the index 熊猫中是否有一种方法可以在一个数据帧中计数(Excel中的Countifs)并在另一个长度不同的数据帧中将计数添加为新列? - Is there a way in Pandas to count (Countifs in excel) in one dataframe and add counts as new column in another dataframe of different length? 从具有相同 df 长度的 numpy 数组的字典中将列添加到 Pandas 数据帧的最快/最佳方法? - Fastest/best way to add columns to a pandas dataframe from a dict of numpy arrays with the same df length? 需要将pandas dataframe列中的可变长度数据拆分为多个列 - Need to split variable length data in a pandas dataframe column into multiple columns Pandas使用新长度插入数据帧 - Pandas Interpolate dataframe with new length 如何从 Python(熊猫或 Numpy)中长度不等的一列生成三列新列 - How to produce three new columns from one column that has unequal length in Python (pandas or Numpy)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM