Reputation: 1
I have a csv file that I am reading in, I have a column of numerical strings and I'm trying to get the difference between the two subsequent rows. The numbers were in depths with "ft" following the values (ex. 4.23ft), I was able to get rid of the "ft" (ex. 4.230), but can't figure out how to assign the values so I can do the math.
depth = float(depth)
rate=0
'''Need to find a way to subtract next line from current line to find
rate of change over 15 minute period'''
for i, data in enumerate(depth):
d1=i
d2=i+1
while rate == 0:
rate = d1-d2
print(rate)
This gives me a TypeError of " 'float' object is not iterable". when I have the "depth = float(depth)" line commented out, I only get -1 values, which I understand the issue there.
first few lines of raw data first few lines of result data
second row first value minus second value equals first value in third row.
Upvotes: 0
Views: 496
Reputation: 6639
Since you have already stripped the "ft" part from your column and assuming you have converted the remaining part of the string to float type, I will jump into the next part directly.
If I understand what you want to achieve correctly, you can also use pandas.DataFrame.shift:
df = pd.DataFrame()
df['D1'] = [1.0, 2.0, 3.0, 4.0, 5.0]
Your D1 the value from current row, D2 will be the column from D1 by performing shift operation.
df['D2'] = df['D1'].shift(-1)
Your dataframe will now look like:
D1 D2
0 1.0 2.0
1 2.0 3.0
2 3.0 4.0
3 4.0 5.0
4 5.0 NaN
In short, you have the values from the subsequent row of the current row into a new column. You can now perform subtraction/difference operation between the two columns as usual. For example:
df['D3'] = df['D1'] - df['D2']
or
df['D3'] = df['D1'].sub(df['D2'])
Upvotes: 1