Python | Pandas Series.astype() to convert Data type of series Last Updated : 01 Oct, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas astype() is the one of the most important methods. It is used to change data type of a series. When data frame is made from a csv file, the columns are imported and data type is set automatically which many times is not what it actually should have. For example, a salary column could be imported as string but to do operations we have to convert it into float. astype() is used to do such data type conversions. Syntax: DataFrame.astype(dtype, copy=True, errors='raise') Parameters: dtype: Data type to convert the series into. (for example str, float, int) copy: Makes a copy of dataframe/series. errors: Error raising on conversion to invalid data type. For example dict to string. 'raise' will raise the error and 'ignore' will pass without raising error. Return type: Series with changed data types To download the data set used in following example, click here. In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below. Example: In this example, the data frame is imported and .dtypes is called on the data frame to view the data types of series. After that some columns are converted using .astype() method and the dtypes are viewed again to see the changes. Python3 1== # importing pandas module import pandas as pd # reading csv file from url data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # dropping null value columns to avoid errors data.dropna(inplace = True) # storing dtype before converting before = data.dtypes # converting dtypes using astype data["Salary"]= data["Salary"].astype(int) data["Number"]= data["Number"].astype(str) # storing dtype after converting after = data.dtypes # printing to compare print("BEFORE CONVERSION\n", before, "\n") print("AFTER CONVERSION\n", after, "\n") Output: As shown in the output image, the data types of columns were converted accordingly. Comment More infoAdvertise with us Next Article Convert a series of date strings to a time series in Pandas Dataframe K Kartikaybhutani Follow Improve Article Tags : Misc Python Python-pandas Python pandas-series Python pandas-series-methods +1 More Practice Tags : Miscpython Similar Reads How to convert a Pandas Series to Python list? In this article, we will discuss how to convert a Pandas series to a Python List and it's type. This can be done using the tolist() method.Example 1: Python3 import pandas as pd evenNumbers = [2, 4, 6, 8, 10] evenNumbersDs = pd.Series(evenNumbers) print("Pandas Series and type") print(evenNumbersDs) 2 min read Convert Series of lists to one Series in Pandas In this program, we will see how to convert a series of lists of into one series, in other words, we are just merging the different lists into one single list, in Pandas. We will be using the stack() method to perform this task. The line will be Series.apply(Pandas.Series).stack().reset_index(drop = 1 min read Convert the data type of Pandas column to int In this article, we are going to see how to convert a Pandas column to int. Once a pandas.DataFrame is created using external data, systematically numeric columns are taken to as data type objects instead of int or float, creating numeric tasks not possible. We will pass any Python, Numpy, or Pandas 2 min read Convert a series of date strings to a time series in Pandas Dataframe During the analysis of a dataset, oftentimes it happens that the dates are not represented in proper type and are rather present as simple strings which makes it difficult to process them and perform standard date-time operations on them. pandas.to_datetime() Function helps in converting a date str 3 min read How To Convert Data Types in Python 3? Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of 4 min read Converting Series of lists to one Series in Pandas Let us see how to convert a Series of lists to a single Series in Pandas. First of all let us create a Series of lists. For this problem we have created a Series of list having 12 elements, each list is of length 3 and there are 4 lists hence total elements are 12. When the Series of list will be co 2 min read Like