'Comparison of two values from a column in pandas dataframe causing a TypeError

I'm trying to build a new column in my pandas dataframe, raw_data, called CyclesTest. I want it to contain a value, cycle_test, that begins at 1. Every row of this column will contain the number 1, until a condition is met in a comparison of the values in another column of the dataframe, Step, where the value of the next 'step' is less than the one that the if loop has reached. At that point, CyclesTest would increase by 1, and continue to the end of the dataframe.

This is my code so far:

raw_data['CyclesTest'] = " "
cycle_test = 1

for i in (raw_data.index):
    if  raw_data['Step'][i] > raw_data['Step'][i+1]:
        raw_data['CyclesTest'][i] = cycle_test
        cycle_test+=1
    else:
        raw_data['CyclesTest'][i] = cycle_test
        
    
print(incefa_raw_curve_data['CyclesTest'])

However, I get this error on the comparison line:

if  raw_data['Step'][i] > raw_data['Step'][i+1]:

TypeError: '>' not supported between instances of 'int' and 'str'

This is an output of the column, Step

print(raw_data['Step'])

0        1
1        1
2        1
3        1
4        1
        ..
51790    7
51791    7
51792    7
51793    7
51794    7

Why would I get the comparison error if step only contains integer values? I tried to cast both values to int which just caused more errors. Any help would be greatly appreciated!



Solution 1:[1]

pls explain the code. show all your code so i can understand what are you trying to do

raw_data = {"Step":[1,2,3,4,5]}
raw_data['CyclesTest'] = {}
cycle_test = 1
try:
    for i in range(1,11):
        if  raw_data['Step'][i] > raw_data['Step'][i+1]:
            raw_data['CyclesTest'][i] = cycle_test
            cycle_test+=1
        else:
            raw_data['CyclesTest'][i] = cycle_test
except Exception as e:
    print(e)
finally:
    print(raw_data['CyclesTest'])

is that what are you trying to do? i have no idea

raw_data = {"Step":[1,2,3,4,5],"stuff":True,"_":"","dict":{},"list":[],"int":int(0/1),"float":0/1,"str":"abc"}
raw_data['CyclesTest'] = {}
cycle_test = 1
print(raw_data)
for i in range(len(raw_data)-len(raw_data["Step"])):
    print(i)
    if  raw_data['Step'][i] > raw_data['Step'][i+1]:
        raw_data['CyclesTest'][i] = cycle_test
        cycle_test+=1
    else:
        raw_data['CyclesTest'][i] = cycle_test


print(raw_data['CyclesTest'])

Please add more details

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian None