Python Forum
Assigning cycle values in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning cycle values in a list
#2
Can you provide sample input and desired output?

Are time sequence numbers always consecutive in a segment? If so, you could use the shift command to find numbers out of sequence.
import pandas as pd

# Make some data
df = pd.DataFrame({"time":[-2, -1, 0, 1, -1, 0, 1, 0, 1, 2, 3]})

# Make new series that is the time series data down 1 row + 1.  As long as numbers in time
# are sequential, time == shifted time.  Set first value in shifted time to something that won't
# match anything in time.
df["shifted"] = (df.time.shift(1, fill_value=0.5) + 1)

# Find where time != shifted time.  This is the start of a series.
df["mismatch"] = df.time != df.shifted

# Count the mismatches.  This is the series number.
df["series"] = df.mismatch.cumsum()
print(df)
Output:
time shifted mismatch series 0 -2 1.5 True 1 1 -1 -1.0 False 1 2 0 0.0 False 1 3 1 1.0 False 1 4 -1 2.0 True 2 5 0 0.0 False 2 6 1 1.0 False 2 7 0 2.0 True 3 8 1 1.0 False 3 9 2 2.0 False 3 10 3 3.0 False 3
Doing it all in one line.
import pandas as pd

df = pd.DataFrame({"time":[-2, -1, 0, 1, -1, 0, 1, 0, 1, 2, 3]})
df["series"] = (df.time != (df.time.shift(1, fill_value=0.5) + 1)).cumsum()
print(df)
Output:
time series 0 -2 1 1 -1 1 2 0 1 3 1 1 4 -1 2 5 0 2 6 1 2 7 0 3 8 1 3 9 2 3 10 3 3
Reply


Messages In This Thread
Assigning cycle values in a list - by nmancini - Sep-13-2024, 11:12 PM
RE: Assigning cycle values in a list - by deanhystad - Sep-14-2024, 01:53 PM
RE: Assigning cycle values in a list - by nmancini - Sep-16-2024, 08:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove duplicates from dicts with list values wardancer84 27 6,459 May-27-2024, 04:54 PM
Last Post: wardancer84
  Writing a cycle to find the nearest point from the array Tysrusko 0 854 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Copying the order of another list with identical values gohanhango 7 2,831 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 3,011 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 3,658 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 5,772 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 2,070 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Need to fix SyntaxError in cycle try alexfrol86 14 6,327 Mar-27-2022, 07:53 AM
Last Post: stevendaprano
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 21,449 Jan-19-2022, 08:33 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 3,372 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020