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
Related
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
This question already has answers here:
Sorting numbers in QTableWidget work doesnt right Pyqt5
(1 answer)
Sorting in pyqt tablewidget
(4 answers)
Closed 11 months ago.
I have a table with id and this ids start from 1 to 22. When I sort with following code:
a = QSortFilterProxyModel()
a.setSourceModel(model)
self.table_view = QTableView()
self.table_view.setModel(a)
self.table_view.setSortingEnabled(True)
self.table_view.sortByColumn(1, Qt.DescendingOrder)
It sorting as in the picture because 8 is bigger than 6 and 2 it reads as string but the column is integer.
How can I sort the column as integer?
This question already has answers here:
Selecting with complex criteria from pandas.DataFrame
(5 answers)
Closed 3 years ago.
I have a dataframe and I want to delete all rows where column A is equal to blue and also col B is equal to green.
I though the below should work, but its not the case.
Can anyone see the problem
df=df.loc[~(df['A']=='blue' & df['B']=='green')]
You should separate the two propositions:
df1=df.loc[~(df['A']=='blue') & ~(df['B']=='green')]
use eq instead of ==:
df.loc[~(df['A'].eq('blue') & df['B'].eq('green'))]
query
Note the != and or as consequence of De Morgan's Law
df.query('A != "blue" or B != "green"')
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'
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())