Необходимо извлечь последовательность отрицательных значений, где предыдущее отрицательное значение меньше текущего значения, а следующее значение меньше текущего значения
import pandas as pd
# Create the DataFrame with the given values
data = {
'Value': [0.3, 0.2, 0.1, -0.1, -0.2, -0.3, -0.4, -0.35, -0.25, 0.1, -0.15, -0.25, -0.13, -0.1, 1]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Мой код:
# Initialize a list to hold the sequences
sequences = []
current_sequence = []
# Iterate through the DataFrame to apply the condition
for i in range(1, len(df) - 1):
prev_value = df.loc[i - 1, 'Value']
curr_value = df.loc[i, 'Value']
next_value = df.loc[i + 1, 'Value']
# Check the condition
if curr_value < prev_value and curr_value < next_value:
current_sequence.append(curr_value)
else:
# If the current sequence is not empty and it's a valid sequence, add it to sequences list and reset
if current_sequence:
sequences.append(current_sequence)
current_sequence = []
# Add the last sequence if it's not empty
if current_sequence:
sequences.append(current_sequence)
Мой результат:
Extracted Sequences:
[-0.4]
[-0.25]
Ожидаемый результат:
[-0.1,-0.2,-0.3,-0.4]
[-0.15,-0.25]
Подробнее здесь: https://stackoverflow.com/questions/786 ... rent-value
Последовательность извлечения Pandas, где предыдущее значение> текущее значение ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Последовательность извлечения Pandas, где предыдущее значение> текущее значение
Anonymous » » в форуме Python - 0 Ответы
- 29 Просмотры
-
Последнее сообщение Anonymous
-