Python Forum

Full Version: List comprehension not working right
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I have written a small snipet using list comprehension, and is not working properly.
I have a dataset that I would like to iterate by row, and if "Rig Sub State" column contains [5,6,7,10,11] update a list with value of 1.
Update the list until the end of dataset and after that create a column [Orienting_State] with the values from the list.
Unfortunately, when I went and check the results, they are mixed. Sometimes for 5,6,7,10,11 it will show 1, but sometimes it doesn't.
Could you please look at the code and tell me what is wrong?
Thank you.
Here is the code (dataset is too big to add here):

##Orienting Script
import pandas as pd
import numpy as np

df = pd.DataFrame(ALPHA_ANALYTICS_1).sort_values(by=['EPOCH'])

conditions = [5 , 6 , 7 , 10 , 11]
temp_list = []
orienting_lists = []

for idx, row in df.iterrows():
    if row['Rig Sub State'] in conditions:
        temp_list.append(1)
    elif row['Rig Sub State'] == 2:
        temp_list.append(0)
        orienting_lists.append(temp_list)
        temp_list = []
    else: 
        temp_list.append(0)
        orienting_lists.append([0] * len(temp_list))
        temp_list = []
        
orienting = [item for sublist in orienting_lists for item in sublist]
Orienting_State = pd.Series(orienting)
(Nov-01-2024, 08:13 PM)Cris9855 Wrote: [ -> ]dataset is too big to add here

I wouldn't want your real dataset. What would be good is to include a small, test datasest. Probably with 2 rows (one that does what you expect and one that doesn't do what you expect). Include the data, the output you're getting and what you expect the output to be instead.
(Nov-01-2024, 10:05 PM)bowlofred Wrote: [ -> ]
(Nov-01-2024, 08:13 PM)Cris9855 Wrote: [ -> ]dataset is too big to add here

I wouldn't want your real dataset. What would be good is to include a small, test datasest. Probably with 2 rows (one that does what you expect and one that doesn't do what you expect). Include the data, the output you're getting and what you expect the output to be instead.

I added two screenshots with the results. Sometimes when Rig Sub State is [4,5,6, 10,11], the resulting column 'Orienting State' has 0s sometimes 1s. They should be 1s if the Rig Sub State ==2 after [4,5,6,10,11], else should be 0.
For example if I have Rig Sub State==[4,5,6,10,11] followed by Rig Sub State=2, then the Orienting State should be 1, else should be 0.
Hope this explains the logic we want to use to calculate the Orienting State column.
Example to share fake-Date as DataFrame:

ALPHA_ANALYTICS_1 = pd.DataFrame(
  [
    (1,5),
    (2,6),
    (3,7),
    (4,8),
    (5,9),
    (6,10),
    (7,11),
    (8,12),
    (9,13),
  ], columns=("EPOCH", "Rig Sub State"))
This has only two columns.