Dataframe countif function issue [duplicate] - pandas

This question already has an answer here:
counting the amount of True/False values in a pandas row
(1 answer)
Closed 5 years ago.
Return
0.0000
-0.0116
0.0000
0.0100
I have a dataframe of the format above and I am trying to count >0 and <0 with the following code
print ("Positive Returns:")
print((df['Return']>0.0).count())
print ("Negative Returns:")
print((df['Return']<0.0).count())
However both return 5119 which is my whole dataframe length
It is not counting correctly.. can anyone advise please?
Thankyou
*not really a duplicate since I am not asking for true/false value it can be >0.1 for example

Use sum for count boolean Trues which are processed like 1s:
print((df['Return']>0.0).sum())
print((df['Return']<0.0).sum())

Related

Hey can anybody tell me what is the meaning of this? [duplicate]

This question already has answers here:
What is [:,:-1] in python? [duplicate]
(2 answers)
Python Difference between iloc indexes
(1 answer)
Understanding slicing
(38 answers)
Closed 6 months ago.
X = df_census.iloc[:,:-1]
y = df_census.iloc[:,-1]
what is the meaning of [:,:-1] in this and also [:,-1]
Following your example: [:,:-1]
The first argument is : which means to get all rows of the dataframe, and :-1 means to return all columns but the last one
In the case of just -1, it means to get the last column

How to sum column Y for all instances of duplicates in column X in py [duplicate]

This question already has answers here:
How do I Pandas group-by to get sum?
(11 answers)
Closed 1 year ago.
I'm working with data for S&P futures. I have a dataframe of data with every 60min close and the volume traded during each 60min interval. I'd like create a new dataframe to sum up the total volume at each price.
Date
Close
Volume
0
4420
100
1
4420.25
200
2
4420.5
300
3
4420
200
4
4420.75
200
5
4422
300
So for example, for 4420, the total volume would be 300, whereas since there are no duplicates for the rest, their total volume would simply be the volume show.
Sorry if the formatting on this question isn't perfect, new to forums.
Appreciate any help!
Use pandas groupby to perform any type of aggregation groupby
dfg = df.groupby('Close')['Volume'].agg('sum').reset_index()

How to assign a variable in a column given another variable? [duplicate]

This question already has answers here:
Conditional Replace Pandas
(7 answers)
Adding a new pandas column with mapped value from a dictionary [duplicate]
(1 answer)
Closed 3 years ago.
This is my dataset :
Datset example
I want to assign to the different values* such as 'W M I HOLDINGS CORP' and 'MILESTONE SCIENTIFIC INC' another variable in the column 'ticker' in order to have the opportunity to sort them.
In the column 'ticker' I need to add WMIH and WLSS respectively to the two different values.
How can I do that ?
I am going to expect the output with something like this :
Output Example
You should be ok with this. Assuming you have your dataset in df variable.
df.loc[df['comnam'] == 'W M I HOLDINGS CORP', 'ticker'] = 'WMIH'
df.loc[df['comnam'] == 'MILESTONE SCIENTIFIC INC', 'ticker'] = 'WLSS'

Check if pandas column contains all zeros [duplicate]

This question already has answers here:
How do I delete a column that contains only zeros in Pandas?
(4 answers)
Closed 5 years ago.
I'm doing a complex calculation on a data frame that is bound to throw exceptions if all the values in a column are zeros. How to do a quick check whether a column is full of zero? i.e. return True if the column has values other than 0 else False.
you can do something like this:
(df['col'] == 0).all()
This will return True if all values are 0 otherwise it will return false

Oracle PL/SQL number to_char ignores 0's even when decimal part exist [duplicate]

This question already has an answer here:
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
Im using the following code to convert a number(38,2) to the format "XXX.XXX.XXX,XX":
TO_CHAR(number, '999G999G999G999G999G999G999D99')
Although, when the number is 0 or 0.XX the 0 is eaten up :
Input: 0
Result: ",00"
Expected Result: "0,00"
Input: 0.23
Result: ",23"
Expected Result: "0,23"
Why does this happen, is there a way of solving this without manipulating the result string ?
Use 0 instead of 9
TO_CHAR(0.00, '999G999G999G999G999G999G990D99')
0 ensures that if there is no digit in that place it'll display 0.