微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在 csv 文件中找到峰值并将峰值添加到同一个 csv 文件的单独列中? 不作图

如何解决是否可以在 csv 文件中找到峰值并将峰值添加到同一个 csv 文件的单独列中? 不作图

这是我的一段代码,但我不知道如何将这个数组作为新列的高度转换为格式的原始csv文件

日期 级别 高度
01-01-2021 45 0
02-01-2021 43 0
03-01-2021 47 1
04-01-2021 46 0
.....
import pandas as pd
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

bestand = pd.read_csv('file.csv',skiprows=1,usecols=[0,1],names=['date','level'])
bestand = bestand['level']

indices = find_peaks(bestand,height= 37,threshold=None,distance=None)

height  = indices[1]['peak_heights']
print(height)

解决方法

我认为您想分配一个名为 height 的列,当 level 是根据 find_peaks() 的峰值时,该列取值为 1。如果是这样:

# Declare column full of zeros
bestand['height'] = 0
# Get row number of observations that are peaks
idx = find_peaks(x=bestand['level'],height=37)[0]
# Select rows in `idx` and replace their `height` with 1
bestand.iloc[idx,2] = 1

返回这个:

         date  level  height
0  01-01-2021     45       0
1  02-01-2021     43       0
2  03-01-2021     47       1
3  04-01-2021     46       0
,

我不确定我是否理解您的问题。

您只想保存结果?

bastand['Heigth'] = indices
bastand.to_csv('file.csv') 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。