0
Follow
2
View

# Iterate over rows of a dataframe based on index in python

csdage007 注册会员
2023-01-26 02:51

If you just want to normalise, you can write the expression directly, using `Series.min` and `Series.max`:

``````m = df['time'].min()
df['normtime'] = (df['time'] - m) / (df['time'].max() - m)
``````

However, if you want the difference between successive elements, you can use `Series.diff`:

``````df['difftime'] = df['time'].diff() / (df['time'].max() - df['time'].min())
``````

Testing:

``````df = pd.DataFrame({'time': [0.000000, 0.020373, 0.040598], 'velocity': [0.136731, 0.244889, 0.386443]})
print(df)
#        time  velocity
# 0  0.000000  0.136731
# 1  0.020373  0.244889
# 2  0.040598  0.386443

m = df['time'].min()
df['normtime'] = (df['time'] - m) / (df['time'].max() - m)

df['difftime'] = df['time'].diff() / (df['time'].max() - df['time'].min())

print(df)
#        time  velocity  normtime  difftime
# 0  0.000000  0.136731  0.000000       NaN
# 1  0.020373  0.244889  0.501823  0.501823
# 2  0.040598  0.386443  1.000000  0.498177
``````
cy199025 注册会员
2023-01-26 02:51

You can use `shift` (see the doc here) to create lagged columns

``````df['time_n-1']=df['time'].shift(1)
``````

Use `df['column']=df['column'].fillna(0)` after your calculations

dsyy80 注册会员

Publish Time
2023-01-26 02:51
Update Time
2023-01-26 02:51