简体   繁体   English

将列向量添加到不同长度的 dataframe

[英]Add column vector to a dataframe of different length

I have a time series, we can call D1, with 63000 rows and 2 columns:我有一个时间序列,我们可以调用 D1,有 63000 行和 2 列:

        Time        pH
0          1  5.818488
1          2  5.821478
2          3  5.817494
3          4  5.816903
4          5  5.817778
...      ...       ...
63895  63896  5.759683
63896  63897  5.762894
63897  63898  5.759819
63898  63899  5.765182
63899  63900  5.764498

And I have a column vector, we can call D2, of 1050 rows:我有一个列向量,我们可以称之为 D2,有 1050 行:

      Bit
0       1
1       0
2       1
3       0
4       0
...   ...
1045    0
1046    0
1047    0
1048    0
1049    0

I want to add the Bit column of D2 to D1 in this way: first value of D2 assigned to first 60 rows of D1, second value of D2 assigned to rows 61-120, and so on:我想以这种方式将 D2 的 Bit 列添加到 D1:D2 的第一个值分配给 D1 的前 60 行,D2 的第二个值分配给第 61-120 行,依此类推:

        Time        pH        Bit
0          1  5.818488          1
1          2  5.821478          1
2          3  5.817494          1
3          4  5.816903          1
4          5  5.817778          1
...      ...       ...
63895  63896  5.759683          0
63896  63897  5.762894          0
63897  63898  5.759819          0
63898  63899  5.765182          0
63899  63900  5.764498          0

How can I do this?我怎样才能做到这一点?

Simple example, np.repeat() does what you need简单的例子, np.repeat()做你需要的

D2 = np.array([1,2])
np.repeat(D2,60)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 PySpark 数据框中添加不同长度的列作为新列 - Add column with different length as new column in PySpark dataframe Python:将一列添加到 dataframe 中,具有不同的长度重复添加的列直到填充 dataframe 长度 - Python : Add a column into a dataframe with different length repeating the added column till fill the dataframe length 如何在 python 中向 dataframe 中添加不同列长度的页脚(行)? - How to add a footer(row) to the dataframe with different column length in python? Pandas:将系列添加到数据框作为列(相同的索引,不同的长度) - Pandas: Add series to dataframe as a column (same index, different length) Python-将numpy数组作为列添加到具有不同长度的pandas数据帧 - Python - add a numpy array as column to a pandas dataframe with different length 匹配 2 个不同的 dataframe 但长度不同的列值 - Match column values of 2 different dataframe but of different length 熊猫中是否有一种方法可以在一个数据帧中计数(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? 使用不同长度的给定列表使 dataframe 具有不同的列长度 - Make dataframe having different column length with the given lists of different length 如何打开具有不同长度元组的 dataframe 列? - How to unpack a dataframe column with tuples of different length? 将具有不同长度的列表添加为数据帧的新列 - Adding list with different length as a new column to a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM