Python 更改数据帧中特定行的增量值

Python 更改数据帧中特定行的增量值,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我有这个数据框: Power 15 15 10 30 15 90 100 22 15 Seconds Power 10 15 20 15 30 10 40 30 50 15 60 90 70 100 80 22 90 15 我想创建另一个名为“Seconds”的列,

我有这个数据框:

   Power
    15
    15
    10
    30 
    15 
    90
    100
    22
    15
Seconds   Power
  10       15
  20       15
  30       10
  40       30
  50       15
  60       90
  70       100
  80       22
  90       15
我想创建另一个名为“Seconds”的列,每行递增10,因此我编写了以下代码:

df.index += 1
df['Seconds'] = 10 * df.index.values
这将生成数据帧:

   Power
    15
    15
    10
    30 
    15 
    90
    100
    22
    15
Seconds   Power
  10       15
  20       15
  30       10
  40       30
  50       15
  60       90
  70       100
  80       22
  90       15
现在我想让Seconds列增加10,直到第5行。在第5行,我想将增量更改为0.1,直到第7行。在第7行,我想把增量改回10

因此,我希望数据帧如下所示:

Seconds   Power
  10       15
  20       15
  30       10
  40       30
  50       15
  50.1     90
  50.2     100
  60.2      22
  70.2      15
我该怎么做呢?当我到达增量需要更改的行时,是否应该更改索引并将index.values乘以其他值


提前感谢您的帮助。

您可以使用
numpy。重复
+
cumsum



从更一般的意义上讲,不必手动计算重复次数可能更直观,而且更容易定义重复次数,而不是根据重复次数与先前值的差异,而是根据阵列中的绝对位置

使用一些算法,我们可以创建一个函数,为您完成所有的grunt工作

def cumsum_custom_increment(increments, points, n):
    points[1:] = np.diff(points)
    d = np.r_[points, n - np.sum(points)]
    return np.repeat(increments, d).cumsum()

>>> cumsum_custom_increment([10, 0.1, 10], [5, 7], df.shape[0])
array([10. , 20. , 30. , 40. , 50. , 50.1, 50.2, 60.2, 70.2])