How to Resolve Typeerror: Can Only Concatenate Str (Not "Int") to Str

Whas might be causing 'TypeError: can only concatenate str (not int) to str' when calculating correlation?

With a little help I was able to solve this, so I am posting an answer. Maybe someone will find it useful.

The problem was indeed related to datatypes. It was caused by non-numeric values in columns SMA14 and SMA50 (those were like 'nan5', 'nan6' etc, not just 'nan'). When checking data.info(), it stated that dtype for mentioned columns are 'Objects'. And we needed them to be float64 instead.

So here what needed to be done.

1). Fix the amounts ('nan5', 'nan6' etc)

to_fix = ('SMA14', 'SMA50')

for col in to_fix:
for el in data[col]:
if (type(el) != int) and (type(el) != float) and (el[0:3] == 'nan'):
index = data[data[col] == el].index.item()
data.at[index, col] = 0

I know there are other ways of doing it, but I've ran into troubles and ultimately what worked for me, was the above. Of course, it might be that I was doing something wrong.

2). Change the datatypes - and this is what I was missing when I asked the question.

data['SMA14'] = data['SMA14'].astype(float)
data['SMA50'] = data['SMA50'].astype(float)

Why does this error happen? TypeError: can only concatenate str (not int) to str

You are trying to add int to string

try this

if a=="yes"or a=="y":
value1=random.randint(0,100)
choice1=input("Your Value is: \n"+str(value1)+ "\nYou would like to make it your Intelligence,Attack Or Defence level? ")

TypeError: can only concatenate str (not int) to str but when trying to change var's to str, I get a different error

I already fixed the problem. The problem being I needed to change
Time
and
Pieces
To
float(time)
and
float(pieces) and it works.

Python: TypeError: can only concatenate str (not int) to str : variable stored wrong

You can concate only strings, so before concate all your userinputs you must "convert" them into strings

Example1:

command1 = "RAMP " + " ".join(map(str, [userinput5, userinput1, userinput2, userinput4, userinput3]))

Example2:

command1 = f"RAMP {userinput5} {userinput1} {userinput2} {userinput4} {userinput3}"

Getting Can only concatenate str (not int) to str when adding to a value read from a file

When you read from a file with f.read, the information acquired is determined as a string, which is why when you try to add 1 to counter ( f.e 5 + 1), the program thinks you are trying to do something like this "5" + 1.

A simple fix would be to state that what you read from the file is an integer:

aaa = int(float(f.read()))


Related Topics



Leave a reply



Submit