Reducing Amount of Categorical Data Categories by Portion of a String

114 views Asked by At

I am attempting to sort some categorical data by iterating through a list of strings and am kind of a novice at doing more text based applications in Python. I am attempting to codify some of the categories with a numerical value, as there are some redundancies and too many categories but with similar string notation. I have tried to iterate across the list, and when I do a simple print of each value where the condition is true, the function works just fine, as noted below:

TwoKorE="Serial-Two"
lst=data['stuff'].tolist()
x=len(TwoKorE)

twolist=[]

for value in lst:

    if value[0:x]==TwoKorE:

        print(value)

However, I would like to append a new categorical variable to the list twolist similar to as below:

TwoKorE="Serial-Two"
lst=data['stuff'].tolist()
x=len(TwoKorE)

twolist=[]

for value in lst:

    if value[0:x]==TwoKorE:

        twolist.append(1)

What am I doing wrong? Any feedback will be much appreciated!

0

There are 0 answers