I am trying to use geopy to reverse fetch location deatils based off of location coordinates. Right now I have Lat and Long in two different columns in pandas and both are of float type. Now to use locator.reverse(''), the input must be a string. And when I try to cast float to string, I am losing some information in the form of changed numbers like
df.Lat[0] = 42.279971
df.Lat.astype('str')
df.Lat[0] = 42.27997063
Why is it rounding off? I need exactly the float number as it is given to me?
Thanks for helping!
In your case here, you are not losing precision by converting float to string. Instead, it is because Pandas defaults to show only 6 decimal points for float numbers. When you convert it to string, this output default is not applied.
You can test it by e.g. to set the default float precision to 8:
pd.set_option("precision", 8)
Then, you will see that before the string conversion, the values is already in that precision already.
I am not sure it can help or not, it convert all cells in dataframe into string
df = df.applymap(lambda x: str(x))
Use repr() to convert float to string without losing precision:
Try:
df.Lat = df.Lat.apply(lambda x: repr(x))
Or
df.Lat = df.Lat.apply(repr)
Related
When I load xlsx file. I have a number IBAN with scientific notation.
For example:
7.810500161524e+25
This number should be:
78105001615240000000000000
I want to convert this number to string, but I get result:
'7.810500161524e+25'
I thought if I converted to int and then to string it would be the correct result, but again I get wrong result:
'78105001615239996483043328'
Anyone have any idea?
You can convert your string '7.810500161524e+25' to a decimal.Decimal number without altering its precision at all the way you will if you convert it to a floating point value. Then (if necessary) you can convert the decimal value to an integer or a string.
import decimal
scientific_notation = '7.810500161524e+25'
decimal_number = decimal.Decimal(scientific_notation) # Decimal('7.810500161524E+25')
int_number = int(decimal_number) # 78105001615240000000000000
fixed_point_string = format(decimal_number, 'f') # '78105001615240000000000000'
I have a string with float numbers where decimal separator is dot like 1.2, 4.2 and so on. And also other numbers where the decimal separator is comma like 2,5, 8,3 and so on. I would like to convert everything to float numbers with dot as decimal separator. I am trying with the following code
df["numbers"].apply(lambda x: x if x.is_float() else x.replace(',','.'))
When excecuting that line I receive a message that says: 'str' object has no attribute 'is_float'
Can anyone help me?
Thanks
Python strings have no attribute is_floatIf you want python to detect whether a string is a float, you can use str.isdecimal().
Note: You don't need that if statement. If the string doesn't contain a ',', calling str.replace(',','.') will not trigger an error, it will just leave it as it is.
Convert x to float after replacing, so that it always is a float number instead of a string, try:
df["numbers"].apply(lambda x: x if x.is_float() else float(x.replace(',','.')))
I want to convert string numbers on a list to float numbers
and i can't do it, so i need some help.
num = '0.00003533'
print('{:f}'.format(float(num)))
formatting it without decimals, only returns a float of 0.000035, i need the entire string in a float.
print('{:8f}'.format(float(num)))
adding the exact decimal works, but the numbers in the list with decimals varies greatly, so i can't manually add it everytime, how could i automatically add the correct decimal number inside the format?
something like '{':exactdecimalf'} exactdecinal being a variable.
i'm using a module that requires float, which is why i can't print it directly from the string format.
Use this
from decimal import Decimal
num = '0.00003533'
print(Decimal(num)) #0.00003533
if you want to print as string
print ('{:f}'.format(Decimal(num)))
Maybe double precision will suit you.
from decimal import Decimal
print ('{:f}'.format(Decimal(num)))
You can split the string and take the length of the last part with
len(num.split(".")[1])
Then use that as the number of decimals.
I'm doing some calculations which give very small decimal numbers for example, 0.0000082
When I'm saving it in a variable, it changes into exponent form. I need the result as a string in the end. So, converting the result using str() is not possible because it keeps the e in the string.
I need the string to have exactly 8 decimal places. Is there any way to do this while keeping the 8 digit precision intact?
Another example: 5.8e-06 should be converted to '0.00000580' The trailing zero in the final string is not important.
I need the string to be used elsewhere. So, this shouldn't be done in the print() function.
The exponential notation is not an inherent property of the number (which is stored as a binary floating point value). It's just the default representation when converting the number to a string with str. You can specify your own formatting options if you convert the number to a string using the format function. Try something like this:
format(5.8e-06, '.8f')
The 8 in the format specifier tells it to use eight digits of precision, while the f requests it to be written as a plain decimal without exponential notation. You can read more about the format notations in the documentation.
Just another idea:
'{0:.7f}'.format(0.0000082)
you can try with :
import decimal
print(str(decimal.Decimal(5.8e-06))[:10])
>>> 0.00000580
print ("{:.6f}".format(1e-4))
will print out
0.000100
You could use print:
>>> number = 1e-08
>>> number
1e-08
>>>print("{:.12f}".format(float(number)))
0.000000010000
or You could convert number and store it in string:
>>> str1 = "{:.12f}".format(float(number))
>>> str1
'0.000000010000'
Does anyone know how to convert int to float.
For some reason, it keeps on printing 0. I want it to print a specific decimal.
sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / np.size(women_onboard)
print 'Proportion of women in class3 who survived is %s' % proportion_womenclass3_survived
To convert an integer to a float in Python you can use the following:
float_version = float(int_version)
The reason you are getting 0 is that Python 2 returns an integer if the mathematical operation (here a division) is between two integers. So while the division of 144 by 314 is 0.45~~~, Python converts this to integer and returns just the 0 by eliminating all numbers after the decimal point.
Alternatively you can convert one of the numbers in any operation to a float since an operation between a float and an integer would return a float. In your case you could write float(144)/314 or 144/float(314). Another, less generic code, is to say 144.0/314. Here 144.0 is a float so it’s the same thing.
Other than John's answer, you could also make one of the variable float, and the result will yield float.
>>> 144 / 314.0
0.4585987261146497
In Python 3 this is the default behavior, but if you aren't using that you can import division like so:
>>> from __future__ import division
>>> 144/314
0.4585987261146497
Alternatively you can cast one of the variables to a float when doing your division which will do the same thing
sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / float(np.size(women_onboard))
You can just multiply 1.0
>>> 1.0*144/314
0.4585987261146497
You can literally convert it into float using:
float_value = float(integer_value)
Likewise, you can convert an integer back to float datatype with:
integer_value = int(float_value)
Hope it helped. I advice you to read "Build-In Functions of Python" at this link:
https://docs.python.org/2/library/functions.html
The answers provided above are absolutely correct and worth to read but I just wanted to give a straight forward answer to the question.
The question asked is just a type conversion question and here its conversion from int data type to float data type and for that you can do it by the function :
float()
And for more details you can visit this page.