Home > front end >  How do I turn a float into a stringer in python?(Jupyter Notebook/Google Colab)
How do I turn a float into a stringer in python?(Jupyter Notebook/Google Colab)

Time:06-25

How do I turn a a float into a string? Hey guys, I've been trying to turn the column ileads_address into a string. In all the addresses, there's a .0 behind them. But when I did turn the float into the string, it didn't take away the 0s. Anyone know how to fix this.

'''df4['ileads_address'] = df4['ileads_address'].astype(str)'''

[-----> csv sample file here][1]

edit: Yes I am looking for an integer representation! [1]: https://i.stack.imgur.com/gyJJJ.png

CodePudding user response:

Use astype() for type casting.

If column ileads_address is a float:

df4['ileads_address'] = df['ileads_address'].astype(int).astype(str) 

First you type cast into integer, which removes the .0, then into string.

If column ileads_address is a str:

df4['ileads_address'] = df['ileads_address'].astype(float).astype(int).astype(str) 

First you type cast into float, then integer, which removes the .0, then into string.

Reproduceable example:

import pandas as pd

df = pd.DataFrame({'A':['1.3','1.4','1.6']})
print(df)

df['A'] = df['A'].astype(float).astype(int).astype(str)
print(df)

CodePudding user response:

Try this:

df4['ileads_address'] = df['ileads_address'].apply(str) 
  • Related