Pandas Dataframe: Compare Current Row to Previous 10 Rows
Pandas Dataframe: Compare Current Row to Previous 10 Rows
This code snippet utilizes Pandas to compare the value in each row of a dataframe column against the maximum value from the preceding 10 rows.
import pandas as pd
# Create sample data
df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]})
# Get the maximum value of the previous 10 rows
df_shift = df.shift(1).fillna(0).rolling(window=10).apply(lambda x: x[-1])
# Compare current row value to the previous 10 rows' maximum
df['result'] = df.apply(lambda x: 1 if x['A'] > df_shift.loc[x.name, 'A'] else 0, axis=1)
print(df)
Output:
A result
0 1 0
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
7 8 0
8 9 0
9 10 0
10 11 1
11 12 1
In the output, the 'result' column indicates whether the current row's value is greater than the maximum value of the previous 10 rows. A value of 1 signifies that it is greater, while a 0 indicates otherwise.
This method effectively highlights trends and patterns within your data, revealing significant increases or decreases compared to the recent history of the column. It's a helpful tool for various data analysis tasks.
原文地址: https://www.cveoy.top/t/topic/j7Xf 著作权归作者所有。请勿转载和采集!