TypeError: cannot convert the series to class int ( Solved )

TypeError_ cannot convert the series to class int ( Solved )

Pandas package allows you to convert any existing data into dataframe. After that, you can easily manipulate any columns using different inbuilt functions. You can select any columns and convert them into series using the square bracket like df[“your_colum”]. Let’s say you want to convert the data type of the values of the column to int then how you can do so? Most coders are unable to typecast the columns properly. They may get error like TypeError: cannot convert the series to class int. In this post, you will know how to solve this error.

What causes TypeError: cannot convert the series to class int?

The main reason you are getting the error is that you are unable to properly typecast the Series. You already know that any column you choose has its values in the Series. If you typecast the series using the int() function then it will not work.

You will get the python typeerror when you will run the below lines of code.

import pandas as pd
data = {"name":["Rob","Bam","Rob","Rahul"],"age":[23,25,23,32],
"country":["USA","UK","USA","Germany"]}
df = pd.DataFrame(data)
df["age"] = df["age"].astype(int)
df

Output

cannot convert the series ti class int error
cannot convert the series to class int error

Here you can see I am typecasting the values of the age column and I am getting the error.

Solve cannot convert the series to class int error

The solution for this error in python is very simple. You must try to convert the type of the data frame column properly. There are two ways you can convert series to integer data types. The first one is using the astype() function and the other is to_numeric() function.

Solution 1: Use astype() method

The first solution is to convert the series to integer type using the astype() function. Just choose the column and use invoke the function using the dot operator.

Run the below lines of code.

import pandas as pd
data = {"name":["Rob","Bam","Rob","Rahul"],"age":[23,25,23,32],
"country":["USA","UK","USA","Germany"]}
df = pd.DataFrame(data)
df["age"] = df["age"].astype(int)
df

Output

typecasting age column using astype function
typecasting age column using astype function

Solution 2: Apply the to_numeric() function

The second solution is to use the to_numeric() method. Just pass the column as the argument to this function and all the column values will be converted to the integer type.

import pandas as pd
data = {"name":["Rob","Bam","Rob","Rahul"],"age":[23,25,23,32],
"country":["USA","UK","USA","Germany"]}
df = pd.DataFrame(data)
df["age"] = pd.to_numeric(df["age"])
df

Output

typecasting age column using to_numeric function
typecasting age column using the to_numeric function

Now, You can see you are not getting cannot convert the series to class int error.

Conclusion

The Python interpreter will raise the error when you will pass all the column values to the int() function. This function works on a single variable value. If you want to convert the series to integer type then use the astype() or pandas.to_numeric() function. If you are getting the error then the above solution will solve it.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner