I am unable to get why point zero is adding at the end of the string post converting from int to string.
The below is the code which I am trying to convert:
df['attendees_num'].astype(str)
here attendees_num value is 20 but its giving output 20.0. can someone help me how do i convert it into 20 as string without .0 at end.?
This should do it:
df['attendees_num'].astype(int).astype(str)
Related
I know this is kind of weird question. I was developing a program, and suddenly I got stuck a bit strange issue. Let me explain the portion, where I am having an issue:
I have int type list variable, where I stored binary values like
Now I want to take as it is and to convert into Decimal format. How should I do it?
You first need to turn the integer to string than again to int using base 2.
Here is an example:
x = 111000111100000
dec = int(str(x), 2)
print(dec) # --> 29152
I've managed to get 2dp format for a number, however, it returns a string. Is there any way to keep float and always have 2dp regardless if there are instances of .00?
average = "{:.2f}".format(10.0)
print(average)
Outputs
'10.00'
However, if I try to convert this to a float using the following
average = float("{:.2f}".format(10.0))
It outputs the following float:
10.0
I've seen other threads that say to use Decimal, but i'm uncertain of how to do this without importing library.
Is there anyway to achieve this without importing any libraries?
Keep it as a float the entire time, at the last step when you want to show it covert it to string with two decimal points. This is not the perfect answer but it will work.
So when I run:
value = long("00000000000000020000000000000002", 16)
I get :
ValueError: Value out of range: 36893488147419103234
I think it's because long can't take such a big hex number, but I'm not sure.
In reality I'm iterating through a file with a large amount of very big hex numbers, but this is just an example of one of the hex numbers I'm trying to parse.
I've tried using lstrip() to remove some of the 0's but it made no difference to the error.
What am I doing wrong?
The error was being caused by the variable I was trying to assign the value to, not the actual long() function.
I am extracting a string out of a JSON document using python that is being sent by an app in development. This question is similar to some other questions, but I'm having trouble just using x = ast.literal_eval('[0448521958, +61439800915]') due to the plus sign.
I'm trying to get each phone number as a string in a python list x, but I'm just not sure how to do it. I'm getting this error:
raise ValueError('malformed string')
ValueError: malformed string
your problem is not just the +
the first number starts with 0 which is an octal number ... it only supports 0-7 ... but the number ends with 8 (and also has other numbers bigger than 8)
but it turns out your problems dont stop there
you can use regex to fix the plus
fixed_string = re.sub('\+(\d+)','\\1','[0445521757, +61439800915]')
ast.literal_eval(fixed_string)
I dont know what you can do about the octal number problem however
I think the problem is that ast.literal_eval is trying to interpret the phone numbers as numbers instead of strings. Try this:
str = '[0448521958, +61439800915]'
str.strip('[]').split(', ')
Result:
['0448521958', '+61439800915']
Technically that string isn't valid JSON. If you want to ignore the +, you could strip it out of the file or string before you evaluate it. If you want to preserve it, you'll have to enclose the value with quotes.
I have just learned Python for this project I am working on and I am having trouble comparing two values - I am using the Python xlwt and xlrd libraries and pulling values of cells from the documents. The problem is some of the values are in the format 'NP_000000000', 'IPI00000000.0', and '000000000' so I need to check which format the value is in and then strip the characters and decimal points off if necessary before comparing them.
I have tried using S1[:3] to get the value without alphabet characters, but I get a 'float is not subscriptable' error
Then I tried doing re.sub(r'[^\d.]+, '', S1) but I get a Typerror: expected a string or buffer
I figured since the value of the cell that is being returned via sheet.cell( x, y).value would be a string since it is alphanumeric, but it seems like it must be returned as a float
What is the best way to format these values and then compare them?
You are trying to get the numbers from the strings in the format shown? Like to get 2344 from NP_2344? If yes then use this
float(str(S1)[3:])
to get what you want. You can change float to int.
It sounds like the API you're using is returning different types depending on the content of the cells. You have two options.
You can convert everything to a string and then do what you're currently doing:
s = str(S1)
...
You can check the types of the input and act appropriately:
if isinstance(S1, basestring):
# this is a string, strip off the prefix
elif isinstance(S1, float):
# this is a float, just use it