OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pandas Extract Sequence where prev value > current value

  • Thread starter Thread starter Divyank
  • Start date Start date
D

Divyank

Guest
Need to extract sequence of negative values where earlier negative value is smaller than current value and next value is smaller than current value

Code:
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)

My Code:

Code:
# 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)

My Output: Extracted Sequences: [-0.4] [-0.25]

Expected Output: [-0.1,-0.2,-0.3,-0.4] [-0.15,-0.25]
<p>Need to extract sequence of negative values where earlier negative value is smaller than current value and next value is smaller than current value</p>
<pre><code>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)
</code></pre>
<p>My Code:</p>
<pre><code># 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)
</code></pre>
<p>My Output:
Extracted Sequences:
[-0.4]
[-0.25]</p>
<p>Expected Output:
[-0.1,-0.2,-0.3,-0.4]
[-0.15,-0.25]</p>
 

Latest posts

Top