This question already has answers here:
Safest way to convert float to integer in python?
(9 answers)
How do I parse a string to a float or int?
(32 answers)
How do you round UP a number?
(28 answers)
Closed 2 years ago.
I used the below code to calculate the average of an attribute
from pyspark.sql import functions as F
from pyspark.sql.functions import mean
result = df.select([mean("Age")])
result.show()
I got the output as 56.4567 i need to convert it into an integer
If you want the result as int and not df run
result = round(df.select(mean("Age")).collect()[0][0])
result will be of int type.
result_as_integer = int(result)
or
result_as_float = float(result)
First you need to convert pyspark dataframe result to real number:
result = result.take(1)[0].asDict()['avg(Age)']
or
result = result.collect()[0]['avg(Age)']
or
result = result.collect()[0][0]
if you need the floor of the number:
import math
math.floor(float(result))
#56
if you need the ceiling of the number:
import math
math.ceil(float(result))
#57
Related
This question already has answers here:
Converting hex string representation to float in python
(2 answers)
Closed 2 years ago.
Is there a way to reverse the hex() method of a float in Python? For example,
n = 1280.03125
n_hex = n.hex()
print(n_hex) # result--> 0x1.4002000000000p+10
How can I convert 0x1.4002000000000p+10 back to 1280.03125? I know you can use int(num, base) to convert a number to integer but it doesn't support decimal.
Try float.fromhex(str):
>>> float.fromhex("0x1.4002000000000p+10")
1280.03125
This question already has answers here:
Print a float number in normal form, not exponential form / scientific notation [duplicate]
(2 answers)
How to suppress scientific notation when printing float values?
(16 answers)
Closed 2 years ago.
I want to print the whole number instead of 1e-06
number = 1
result = number/1000000
print(result)
Please help whats the best way to do it?
Try out the following by using format:
number = 1
result = number/1000000
print('{0:.6f}'.format(result))
Output:
0.000001
output = f"{num:.9f}"
you can replace 9 with the amount of numbers you have after the decimal point in your number.
and also you will need to define your variable to float to order it will work.
This question already has answers here:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I wish to accept a number in float from the user and give back an answer multiplying that number with pi value = 3.14
heres my code:
print "Enter a no."
n = raw_input('enter here: ')
result = 1
def gen_pi(x):
result = x*3.14
return float(result)
gen_pi(n)
its giving me an error of not being able to multiply non int sequence with float. what does it mean? anything wrong with the code?
The result of raw_input is a str which means that n is a str and then in your function, x is a str which can't multiply a float.
you'll want to convert x into a float before multiplying by pi.
There are lots of places to do this, but I'd recommend doing it before passing to gen_pi:
gen_pi(float(n))
This way, gen_pi can always assume it's working with a float and that code can be simplified:
def gen_pi(x):
return x * 3.14
Also note that if you want more precision (and possibly a bit more code clarity), you can use math.pi:
import math
def gen_pi(x):
return x * math.pi
This question already has answers here:
Integer square root in python
(14 answers)
Closed 9 years ago.
I am trying to get the square root of a really large number yet I get the error:
deltaSqrt = pow(delta,0.5)
OverflowError: long int too large to convert to float
In my case delta is equal to:
5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216
What should I do to get the square root of this number?
Use decimal:
import decimal
>>> d = decimal.Decimal('5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216')
>>> d.sqrt()
Decimal('7.139694782779097001143800270E+307')
If nothing else works, try this:
http://code.google.com/p/gmpy/