dfahsjdahfsudaf
dfahsjdahfsudaf

Reputation: 481

Changing the Increment Value in Data Frame at Certain Row

I have this data frame:

   Power
    15
    15
    10
    30 
    15 
    90
    100
    22
    15

I wanted to create another column called 'Seconds' that increments by 10 every row so I wrote this code:

df.index += 1
df['Seconds'] = 10 * df.index.values

This produces the data frame:

Seconds   Power
  10       15
  20       15
  30       10
  40       30
  50       15
  60       90
  70       100
  80       22
  90       15

I now want to make the Seconds column increment by 10 until the 5th row. At the 5th row, I want to change the increment to 0.1 until the 7th row. At the 7th row, I want to change the increment back to 10.

So, I want the data frame to look like this:

Seconds   Power
  10       15
  20       15
  30       10
  40       30
  50       15
  50.1     90
  50.2     100
  60.2      22
  70.2      15

How would I go about doing this? Should I change the index and multiply the index.values by a different value when I get to the row where the increment needs to change?

Thanks in advance for your help.

Upvotes: 1

Views: 525

Answers (1)

user3483203
user3483203

Reputation: 51165

You can use numpy.repeat + cumsum


np.repeat([10, 0.1, 10], [5, 2, 2]).cumsum()
#                         ^  ^  ^
#                        5th ^  ^
#                           7th ^
#                               Until end -> df.shape - increments[:-1].sum() more generally

array([10. , 20. , 30. , 40. , 50. , 50.1, 50.2, 60.2, 70.2])

In a more general sense, it is probably more intuitive to not have to calculate out the repeats by hand, and it would be easier to define them not by their difference with the previous value, but by absolute location in the array.

Using some arithmetic, we can create a function that does all of the grunt-work for you.

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])

Upvotes: 3

Related Questions