I am just a beginner. Help me with this

index_count=0
for i in Data_work.values.tolist():
coun=Counter(i)

if i[-1] in "1234567":
    Final_Frame.at[index_count, "Current Level"] = i[-1]+"A"
else:
    Final_Frame.at[index_count, "Current Level"] = i[-1]
for key in coun:
    Final_Frame.at[index_count,key]=coun[key]

index_count+=1

final=pd.concat([Data_fixed,Final_Frame],axis=1)
final.to_excel(“Analysed_Data1.xlsx”)

This is my code, when I am shift+enter this shows-

TypeError Traceback (most recent call last)
Cell In[5], line 6
2 for i in Data_work.values.tolist():
3 coun=Counter(i)
----> 6 if i[-1] in “1234567”:
7 Final_Frame.at[index_count, “Current Level”] = i[-1]+“A”
8 else:

TypeError: ‘in ’ requires string as left operand, not float

It could be that Data_work.values.tolist() holds floating point numbers.

Those numbers are then being held (one at a time) in i:

for i in Data_work.values.tolist():

When you hit the if branch the i[-1] is accessing the last element of i and trying to compare that with the string object "1234567", hence the crash.

Maybe you should be doing an on-the-fly type conversion: if str(i[-1]) in "1234567":

That’s my best guess, given the limited information that you’ve posted.

What does i hold?

Do a print(i) just before the for i in... loop and post back the output.

I can also see that you’ve messed up with coun=Counter(i), which should be count=Counter(i) and from that, I’m guessing that i is some kind of an index?

1 Like