Nov-01-2024, 08:13 PM
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):
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)