Change language

Change data type for one or more columns in Pandas Dataframe

Method # 1: Using

# import pandas as pd

import pandas as pd

 
# sample data frame

df = pd.DataFrame ({

’A’ : [ 1 , 2 , 3 , 4 , 5 ],

’B’ : [ ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ ],

’C’ : [ 1.1 , ’1.0’ , ’1.3’ , 2 5 ]})

 
# convert all columns to string

df = df.astype ( str )

print (df.dtypes)

Output:

# import pandas as pd

import pandas as pd

 
# sample data frame

df = pd.DataFrame ({

  ’ A’ : [ 1 , 2 , 3 , 4 , 5 ],

’B’ : [ ’a’ , ’ b’ , ’c’ , ’ d’ , ’e’ ],

’C’ : [ 1.1 , ’1.0’ , ’ 1.3’ , 2 , 5 ]})

 
# using a dictionary to convert specific columns

convert_dict = { ’A’ : int ,

’C’ : float

}

  

df = df.astype ( convert_dict)

print (df.dtypes)

Output:

Method # 2: Using

# import pandas as pd

import pandas as pd

  
# sample data frame

df = pd.DataFrame ({

  ’ A’ : [ 1 , 2 , 3 , ’4’ , ’5’ ],

  ’ B’ : [ ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ ],

’C’ : [ < code class = "value"> 1.1 , ’2.1’ , 3.0 , ’4.1’ , ’ 5.1’ ]})

 
# using the apply method

df [[ ’A’ , ’ C’ ]] = df [[ ’A’ , ’C’ ]]. apply (pd.to_numeric)

print (df.dtypes)

Output:

Method # 3: Using DataFrame.infer_objects ()
This method attempts to gently convert by inferring the data type of the object type columns. Non-object and non-reversible columns remain unchanged.

# import pandas as pd

import pandas as pd

 
# sample data frame

df = pd.DataFrame ({

’A’ : [ 1 , 2 , 3 , 4 , 5 ],

’B’ : [ ’ a’ , ’b’ , ’c’ , ’d’ , ’ e’ ],

’C’ : [ 1.1 , 2.1 , 3.0 , 4.1 , 5.1 ]

}, dtype = ’object’ )

 
# data type conversion х

df = df.infer_objects ( )

print (df.dtypes)

Output:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically