TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas DataFrame.astype()

Pandas DataFrame.astype() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.astype()

The astype() method is generally used for casting the pandas object to a specified dtype.astype() function. It can also convert any suitable existing column to a categorical type.

It comes into use when we want to case a particular column data type to another data type. We can also use the input to Python dictionary to change more than one column type at once. In the dictionary, the key label corresponds to the column name, and the values label corresponds to the new data types that we want to be in the columns.

Syntax

DataFrame.astype(dtype, copy=True, errors='raise', **kwargs)

Parameters

dtype: It uses numpy.dtype or the Python type for casting the entire pandas object to the same type. It can also use {col: dtype, ?} alternatively where col refers to the column label, and dtype is a numpy.dtype or Python type for casting one or more of the DataFrame's columns to column-specific types.

copy: If copy=True, it returns a copy. Be careful when setting copy= False because changes to values may propagate to other pandas objects.

errors: For provided dtype, it controls the raising of exceptions on the invalid data.

  • raise: It allows the exception that is to be raised.
  • ignore: It ignores the exception. It returns the original object on error.

kwargs: It is a keyword argument that is to be passed on to the constructor.

Returns

casted: It returns the same type as a caller.

Example

import pandas as pd
a = {'col1': [1, 2], 'col2': [3, 4]}
info = pd.DataFrame(data=a)
info.dtypes
# We convert it into 'int64' type.
info.astype('int64').dtypes
info.astype({'col1': 'int64'}).dtypes
x = pd.Series([1, 2], dtype='int64')
x.astype('category')
cat_dtype = pd.api.types.CategoricalDtype(
categories=[2, 1], ordered=True)
x.astype(cat_dtype)
x1 = pd.Series([1,2])
x2 = x1.astype('int64', copy=False)
x2[0] = 10
x1  # note that x1[0] has changed too

Output

0    12
1     2
dtype: int64

Next TopicDataFrame.count()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf