我有一个数据帧df
,包含6000多行数据,日期时间索引的形式为YYYY-MM-DD
,列为ID
、water_level
和change
我想:
change
中的每个值并确定转折点turningpoints_df
turningpoints_df
,这样我就可以得到如下结果: ID water_level change
date
2000-10-01 2 5.5 -0.01
2000-12-13 40 10.0 0.02
2001-02-10 150 1.1 -0.005
2001-07-29 201 12.4 0.01
... ... ... ...
我在考虑采取一种定位方法,例如(纯粹是说明性的):
turningpoints_df = pd.DataFrame(columns = ['ID', 'water_level', 'change'])
for i in range(len(df['change'])):
if [i-1] < 0 and [i+1] > 0:
#this is a min point and take this row and copy to turningpoints_df
elif [i-1] > 0 and [i+1] < 0:
#this is a max point and take this row and copy to turningpoints_df
else:
pass
我的问题是,我不确定如何根据前后的值检查我的change
列中的每个值,然后在满足条件时如何将该行数据提取到新的df中
使用一些NumPy特性,允许您向前或向后
roll()
一系列。然后将prev和next放在同一行上,这样就可以使用一个简单的函数来apply()
您的逻辑,因为所有内容都在同一行上听起来您想使用DataFrame的
shift
方法然后,您应该能够比较每一行的值,并继续进行您试图实现的任何操作
相关问题 更多 >
编程相关推荐