how to convert str in float?
paris=requests.get('https://www.metaweather.com/api/location/search/?query=paris').json()
paris_latlong=paris[0]['latt_long']
a=float(paris_latlong)
print(a)
ValueError: could not convert string tofloat.
Try this:
paris=requests.get('https://www.metaweather.com/api/location/search/?query=paris').json()
paris_latlong=paris[0]['latt_long'].split(',')
a=float(paris_latlong[0])
b=float(paris_latlong[1])
print(a, b)
Related
i'm trying to convert data type from object to float, however when I try to convert it shows the error message:
ValueError: could not convert string to float: '1.318.21'
Here is the code:
profkes_na=profkes_df.fillna(0)
profkes_decimal=profkes_na.stack().str.replace(',','.').unstack()
profkes_float=profkes_decimal.astype('float')
Thank you.
change : profkes_decimal=profkes_na.stack().str.replace(',','.').unstack()
to : profkes_decimal=profkes_na.stack().str.replace(',','').unstack()
It's unclear from the question what modules are being used. In pure Python one could do this:
s = '1.318.21'
def asfloat(s):
if (ndp := s.count('.')) > 1:
s = s.replace('.', '', ndp-1)
return float(s)
print(asfloat(s))
I have been trying to change an object to string, so i first did the below code
total_deliveries_data.Order_ID = map(lambda x : x.str.split('-').str[1].str.split(',').str[0], total_deliveries_data.Order_ID)
total_deliveries_data
when i try the below code to convert the column to in.
total_deliveries_data["Order_ID"] = total_deliveries_data["Order_ID"].astype(str).astype(int)
i get
ValueError: invalid literal for int() with base 10: '<map object at 0x7f9f1f678f10>'
How can i sort this out
I'm trying to add a check here that if the receiver is just a string. then convert that into list else just pass.
if type(receiver) == str:
receiver=[receiver]
error:
TypeError: 'str' object is not callable
You can check the type of an instance with the following.
if isinstance(receiver, str):
# do something if it is a string
If you just need to check rather it is not a string:
if not isinstance(receiver, str):
# do something if it is not a string
Look at this tutorial to learn more about the function.
a = '123'
print(str(a))
print(type(a))
I have converted an output from PyTorch using .detach().numpy() which produces this kind of data :
b'0.06722715'
which is a byte type according to type() from Python. How can I convert this to an integer ?
Try this (explaination in code comments). You can convert 0.06 to an integer but you'll get a zero. Did you mean float?
#byte
b = b'0.06722715'
# to string
s = b.decode()
# to float
f = float(s)
# to integer
i = int(f)
print("Float", f)
print("Integer", i)
or simply
be_float = float(b.decode())
print (be_float)
I'd like know if there is a quick way to accept data types as a user input directly so I can use it directly to cast type on other variables?
I am not looking for conditionals or for loops to check for all possible options.
Thanks for the help in advance.
ex:
data_type = input('Enter a data type: str , bool, int or float: ')
user enters: str
I use datatype variable to change an integer to string type: datatype(234)
The usage of eval is not recommended, but that solves your question:
data_type = eval(input('Enter a data type: str , bool, int or float: '))
But we also have a safer method than eval. That is using the ast library:
import ast
data_type = ast.literal_eval(input('Enter a data type: str , bool, int or float: '))