This question already has answers here:
Absolute value for column in Python
(2 answers)
Closed 5 years ago.
In my dataframe I have a column containing numbers, some positive, some negative. Example
Amount
0 -500
1 659
3 -10
4 344
I want to turn all numbers Df['Amount'] into positive numbers. I thought about multiplying all numbers with *-1. But though this turns negative numbers positive, and also does the reverse.
Is there a better way to do this?
You can assign the result back to the original column:
df['Amount'] = df['Amount'].abs()
Or you can create a new column, instead:
df['AbsAmount'] = df['Amount'].abs()
You can take absolute value
d['Amount'].apply(abs)
abs() is the standard way to get absolute values.
Related
This question already has answers here:
Comparing floats in a pandas column
(4 answers)
Closed 6 months ago.
So I have a dataframe with the index and a column named 0
image1
When I search for the index value of all the rows equal to '0' with
df.index[df[0] == 0].tolist()
It is working no problem
image2
When I search for the index value of a row equal to a specific value such as '0.000376' with df.index[df[0] == 0.000376].tolist(). The output gives me nothing eventhough this value does exist in the data set.
image3
Must be veeery basic but yeah I've been stuck on this for 2 days lol
This is due to floating point approximation, use:
import numpy as np
df.index[np.isclose(df[0], 0.000376)].tolist()
This question already has an answer here:
Pandas Dataframe Comparison and Floating Point Precision
(1 answer)
Closed 6 months ago.
For example:
df[df['nums']==23].head() #gives what its supposed to, but
df[df['nums']==35.0454545455].head() #gives a blank df and that value exists in the column.
The float value is more specific than the filter you are providing.
You could round your float value and compare or >= some value would work. A precise float unless precise to the last decimal will not work.
This question already has answers here:
Pandas how to use pd.cut()
(5 answers)
Closed 6 months ago.
I am using Pandas cut to bin certain values in ranges according to a column. I am using user defined bins i.e the ranges are being passed as array.
df['Range'] = pd.cut(df.TOTAL, bins=[0,100,200,300,400,450,500,600,700,800,900,1000,2000])
However the values I have are ranging till 100000. This restricts the values to 2000 as an upper limit, and I am losing values greater than 2000. I want to keep an interal for greater than 2000. Is there any way to do this?
Let's add np.inf to end of your bin list:
pd.cut(df.TOTAL, bins=[0,100,200,300,400,450,500,600,700,800,900,1000,2000,np.inf])
This question already has answers here:
Pandas conditional creation of a series/dataframe column
(13 answers)
Closed 1 year ago.
My data frame looks like this.
a d e
0 BTC 31913.1123 -6.5%
1 ETH 1884.1621 -18.8%
2 USDT 1.0 0.1%
3 BNB 294.0246 -8.4%
4 ADA 1.0342 -14.3%
5 XRP 1.1423 -10.5%
On column d, I want to round the floats in column d to a whole number if it is greater than 10. If it is less than 10, I want to round it to 2 decimal places. This is the code I have right now df1['d'] = df1['d'].round(2). How do I had a conditional statement to this code to have it round based on conditions?
https://stackoverflow.com/a/31173785/7116645
Taking reference from above answer, you can simply do like following
df['d'] = [round(x, 2) if x > 10 else x for x in df['d']]
You can use simple statements like this:
df1['d'][df1['d']>10]=df1['d'][df1['d']>10].round()
df1['d'][df1['d']<10]=df1['d'][df1['d']<10].round(2)
Use numpy.where:
df1['d'] = np.where(df1['d'] < 10, df1['d'].round(2), df1['d'].round())
This question already has answers here:
Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas
(8 answers)
Closed 2 years ago.
I am trying to build a column that will be based off of another. The new column should reflect the values meeting certain criteria and put 0's where the values do not meet the criteria.
Example, a column called bank balance will have negative and positive values; the new column, overdraft, will have the negative values for the appropriate row and 0 where the balance is greater than 0.
Bal Ovr
21 0
-34 -34
45 0
-32 -32
The final result should look like that.
Assuming your dataframe is called df, you can use np.where and do:
import numpy as np
df['Ovr'] = np.where(df['Bal'] <0,'df['Bal'],0)
which will create a column called Ovr, with 0's when Bal is +ve, and the same as Bal when Bal is -ve.
df["over"] = df.Bal.apply(lambda x: 0 if x>0 else x)
Additional method to enrich your coding skills. However, it isn't needed for such easy tasks.