Python | Pandas Index.astype() Last Updated : 16 Dec, 2018 Comments Improve Suggest changes 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 Index.astype() function create an Index with values cast to dtypes. The class of a new Index is determined by dtype. When conversion is impossible, a ValueError exception is raised. Syntax: Index.astype(dtype, copy=True) Parameters : dtype : numpy dtype or pandas type copy : By default, astype always returns a newly allocated object. If copy is set to False and internal requirements on dtype are satisfied, the original data is used to create a new Index or the original Index is returned. Example #1: Use Index.astype() function to change the data type of index from float to integer type. Python3 # importing pandas as pd import pandas as pd # Creating the Index df=pd.Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10, 5.5]) print("Dtype before applying function: \n", df) print("\nAfter applying astype function:") # Convert df datatype to 'int64' df.astype('int64') Output : Example #2: Use Index.astype() function to change the datatype of the given Index to string form. Python3 # importing pandas as pd import pandas as pd # Creating the Index df=pd.Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10, 5.5]) print("Dtype before applying function: \n", df) print("\nAfter applying astype function:") # Convert df datatype to 'int64' df.astype('str') Output : Example #3: Let's do something interesting with index.astype() method. Observe this DataFrame. Setting 'Number' column as index. Python3 # 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) # Setting Number column as index data = data.set_index('Number') # Setting index as None data.index.names = [None] data.head(5) Output: Now, let's convert the index to integer. Python3 # applying astype on index data.index.astype('int64') Output: Comment More infoAdvertise with us Next Article Python | Pandas Index.astype() S Shubham__Ranjan Follow Improve Article Tags : Technical Scripter Python Python-pandas Python pandas-indexing Practice Tags : python Similar Reads Python | Pandas Index.dtype Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.dtype attribute return the data type (dtype) of the underlying data of the given Index object. Syntax: Index.dtype Parameter : None Re 2 min read Python | Pandas Index.any() 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 Index.any() function checks if any of the elements in the index are true or not 2 min read Python | Pandas Index.data Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.data attribute return the data pointer of the underlying data of the given Index object. Syntax: Index.data Parameter : None Returns : 2 min read Python | Pandas Index.asof() 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 Index.asof() function returns return the label from the index, or, if not prese 2 min read Python | Pandas Index.dtype_str Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.dtype_str attribute return the data type (dtype) of the underlying data of the given Index object as a string. Syntax: Index.dtype_str 2 min read Python | Pandas Index.argsort() 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 Index.argsort() function returns the integer indices that would sort the index. 2 min read Python | Pandas Index.all() 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 Index.all() function checks if all the elements in the index are true or not. I 2 min read Python | Pandas Index.flags Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.flags attribute return the status of all the flags for the given Index object. Syntax: Index.flags Parameter : None Returns : status o 2 min read Python | Pandas Index.equals() 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 Index.equals() function determine if two Index objects contains the same elemen 2 min read Python | Pandas Index.copy() 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 Index.copy() function make a copy of this object. The function also sets the na 2 min read Like