banner



How To Change Float To String In Python

In this position, we'll see different shipway to Convert Floats to String section in Pandas Dataframe? Pandas Dataframe provides the freedom to alteration the information type of column values. We can change them from Integers to Float type, Integer to String, Draw to Integer, Float to String, etc.

There are ternion methods to exchange Float to Strand:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Heighten your Data Structures concepts with the Python DS Naturally. And to begin with your Machine Learning Journeying, join the Machine Learning - Basic Stratum Course

Method 1: Using DataFrame.astype().


Syntax :

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

This is used to cast a pandas object to a specified dtype. This function also provides the capacity to convert any suitable existing column to categorical type.

Example 1: Converting one column from float to string.

Python3

import pandas as pd

data = [[ 'Harvey' , 10 , 45.25 ], [ 'Carson' , 15 , 54.85 ],

[ 'juli' , 14 , 87.21 ], [ 'Ricky' , 20 , 45.23 ],

[ 'Gregory' , 21 , 77.25 ], [ 'Jessie' , 16 , 95.21 ]]

df = pd.DataFrame(data, columns = [ 'Name' , 'Age' , 'Marks' ],

index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ])

print (df.dtypes)

Output:

Now, we change the information character of column 'Marks' from 'float64' to 'object'.

Python3

df[ 'Marks' ] = df[ 'Marks' ].astype( str )

print ()

print (df.dtypes)

df

Output:


Lesson 2: Converting more than ane column from float to string.

Python3

import pandas as pd

data = [[ 'Harvey.' , 10.5 , 45.25 , 95.2 ], [ 'Carson' , 15.2 , 54.85 , 50.8 ],

[ 'juli' , 14.9 , 87.21 , 60.4 ], [ 'Ricky' , 20.3 , 45.23 , 99.5 ],

[ 'Gregory' , 21.1 , 77.25 , 90.9 ], [ 'Jessie' , 16.4 , 95.21 , 10.85 ]]

df = pd.DataFrame(information, columns = [ 'Name' , 'Geezerhoo' , 'Marks' , 'Accuracy' ],

index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ])

print (df.dtypes)

Output:

Now, we change the data type of columns 'Accuracy' and 'Age' from 'float64' to 'object'.

Python3

df = df.astype({ "Age" : 'str' , "Accuracy" : 'str' })

print ()

print (df.dtypes)

df

Yield:

Method 2: Using Series.apply().


Syntax :

DataFrame.apply(func, axis=0, raw=Delusive, result_type=None, args=(), **kwds)        

This method acting allows the users to make it a function and apply it on every single value of the Pandas series.

Deterrent example: Converting column of a Dataframe from swim to string.

Python3

import pandas as pd

data = [[ 'Harvey.' , 10.5 , 45.25 , 95.2 ], [ 'Carson' , 15.2 , 54.85 , 50.8 ],

[ 'juli' , 14.9 , 87.21 , 60.4 ], [ 'Ricky' , 20.3 , 45.23 , 99.5 ],

[ 'Gregory' , 21.1 , 77.25 , 90.9 ], [ 'Jessie' , 16.4 , 95.21 , 10.85 ]]

df = pd.DataFrame(information, columns = [ 'Diagnose' , 'Age' , 'Portion' , 'Accuracy' ],

index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ])

print (df.dtypes)

Output:

Now, we change the data type of column 'Part' from 'float64′ to "object'.

Python3

df[ 'Share' ] = df[ 'Percentage' ]. put on ( str )

print ()

print (df.dtypes)

df

Output:


Method 3: Using Series.map().

Syntax:

Series.map(arg, na_action=None)        

This method is used to map values from two series having one column same.

Example: Converting column of a dataframe from blow to string.

Python3

import pandas as palladium

data = [[ 'Harvey.' , 10.5 , 45.25 , 95.2 ], [ 'Carson' , 15.2 , 54.85 , 50.8 ],

[ 'juli' , 14.9 , 87.21 , 60.4 ], [ 'Ricky' , 20.3 , 45.23 , 99.5 ],

[ 'Gregory' , 21.1 , 77.25 , 90.9 ], [ 'Jessie' , 16.4 , 95.21 , 10.85 ]]

df = pd.DataFrame(information, columns = [ 'Name' , 'Age' , 'Percentage' , 'Truth' ],

index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ])

print (df.dtypes)

Output signal:

Now, we change the data type of pillar 'Age' from 'float64′ to "object'.

Python3

df[ 'Age' ] = df[ 'Age' ]. map ( str )

print ()

print (df.dtypes)

df

Output:


How To Change Float To String In Python

Source: https://www.geeksforgeeks.org/how-to-convert-floats-to-strings-in-pandas-dataframe/

Posted by: duncanpinge1948.blogspot.com

0 Response to "How To Change Float To String In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel