Is it possible to find peaks in a csv file and add the peaks in a separate column in the same csv file? (without plotting)

Question:

This is a piece of my code, but I don’t know how to get this array as new column Height to the original csv file in the format?

Date Level Height
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)
Asked By: Peter

||

Answers:

I’am not sure I understood your question.

You just want to save the results?

bastand['Heigth'] = indices
bastand.to_csv('file.csv') 
Answered By: Felipe Miranda

I think you want to assign a column named height that takes the value 1 when level is a peak according to find_peaks(). If so:

# 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

Which returns this:

         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
Answered By: Arturo Sbr
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.