This question already has answers here:
The right way to round pandas.DataFrame?
(3 answers)
round a single column in pandas
(8 answers)
Closed 2 years ago.
I would like to check how may we use the simple basic round() function to round values in a pandas dataframe to a specific number of decimal points. I kept playing around but I couldn't get it right and I am very new to Python so am not looking for anything too sophisticated.
I understand if you use round(3.986, 2) it will simply output to 2 d.p. as 3.99.
And I know we may access the df values through df.values.
I tried exploring the df.applymap() function too.
Help !
You can use apply on the column which you want to round the numbers
>>> df['Numeric Column'].apply(lambda x : math.round(x,2))
This will give you the intended result
Related
This question already has answers here:
Splitting a number into the integer and decimal parts
(9 answers)
How to get numbers after decimal point?
(37 answers)
Closed 2 years ago.
If I have a float like 32.879, and I want to end up with just 0.879, I can think of a few ways to do it, like:
Convert to string, strip off everything before the ., convert back to float; OR
32.879 - int(32.879)
Both of those feel like hacks. Is there no pure math operation that can do this?
Sort of like using abs() instead of if x < 0: x = x*-1
I'm dealing with Python, but if anyone can tell me the math name for this operation, I can probably google the Python way to do it.
This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]
This question already has answers here:
pandas logical and operator with and without brackets produces different results [duplicate]
(2 answers)
Logical operators for Boolean indexing in Pandas
(4 answers)
Closed 3 years ago.
I was trying to find records based on two conditions on a data frame preg
First:
preg[preg.caseid==2298 & preg.pregordr==1]
This throws and error that truth value of a series is ambiguous.
Why?
Second:
But this one works!
preg[(preg.caseid==2298) & (preg.pregordr==1)]
So what exactly is the difference between the two?
Because it thinks that you're doing 2298 & preg.pregordr something like that, without parenthesis you can do:
preg[preg.caseid.eq(2298) & preg.pregordr.eq(1)]
This question already has answers here:
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed 4 years ago.
How would I format my float digits so that there is always 7 digits no matter the direction?
The value could be any of the following but I always want 7 like below
0.0054233
1234567
123.1224
1.992923
The portion I have right now is
return "%.7f" % fd
How can I adjust that to get my desired output? or maybe link me to something similar I could try and read from..>?
Try if this can work for you:
n = 123.456789
decimals = 7 - len(str(int(n)))
print('{:.{prec}f}'.format(n, prec=decimals))
#=> 123.4568
The drawback is that it rounds up.
It depends on the context of the program, in my opinion... If you just want the numbers to the right to be 6 decimals, use:
"{:.6f}".format(whatevervar)
In other contexts maybe convert it to a string and use len() to evaluate the number of digits.
Hope this helps!
EDIT: Seeing your comments, I would recommend using a conditional to define what you are trying to do. When the number has no decimals (check this thread for how to do it: How to check if a float value is a whole number ) leave it as it is, when it has decimals, round it up with the code I posted above.
This question already has answers here:
Uncomfortable output of mode() in pandas Dataframe
(4 answers)
Closed 4 years ago.
train['Gender'].fillna(train['Gender'].mode()[0], inplace=True)
I got this code in one of my basic data science course. I wanted to understand, what is the significance of "[0]" after mode() in this. I would really appreciate the answer.
Thanks!
Mode documentaion
The mode() return 2 value, first is mode value second is count. So train['Gender'].mode()[0] means get the mode value of train['Gender'].
The notation [0] means that the thing before it (mode() in this case) is a collection, a list, an array, ..., and you are taking the first element.
In case you need more information, you need to include the rest of the source code (preferably by editing your question), explaining the exact meaning of the mentioned objects.