如何比较CSV列数据

我有一个巨大的CSV文件,其中包含2个相关的列。时间和速度。驾驶汽车时创建的数据。

现在,我想比较一下速度列的一些值,以得出汽车是在加速还是在变慢的结论,并将其放入“加速”的新数据框中。

例如:

if speed (row 1) < speed (row2) car is accelerating.


杨__羊羊
浏览 212回答 1
1回答

桃花长相依

使用pd.Series.diff创建布尔系列:df = pd.DataFrame({'speed': [1.41, 5.341, 10.3412, 3.341, 456.432]})df['accelerating'] = df['speed'].diff() > 0print(df)&nbsp; &nbsp; &nbsp; speed accelerating0&nbsp; &nbsp; 1.4100&nbsp; &nbsp; &nbsp; &nbsp; False1&nbsp; &nbsp; 5.3410&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True2&nbsp; &nbsp;10.3412&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True3&nbsp; &nbsp; 3.3410&nbsp; &nbsp; &nbsp; &nbsp; False4&nbsp; 456.4320&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True然后过滤以加速行是微不足道的:df_acc = df[df['accelerating']]print(df_acc)&nbsp; &nbsp; &nbsp; speed accelerating1&nbsp; &nbsp; 5.3410&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True2&nbsp; &nbsp;10.3412&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True4&nbsp; 456.4320&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;True以上是的Pandorable速记df_acc = df.loc[df['accelerating'] == True]。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python