How to delete any row for which a corresponding column has a negative value using Pandas?
You can drop rows for which in a specific column the value is negative using pandas.DataFrame.drop
as follows:
import pandas as pd
df = pd.DataFrame({
'colA': [-1, 2, 3, 4, None],
'colB': [True, True, False, False, True],
})
df = df.drop(df.index[df['colA'] < 0])
Output:
>>> df
colA colB
1 2.0 True
2 3.0 False
3 4.0 False
4 NaN True
Remove all rows which contain at least one negative value in R
Another base R option using rowSums
+ sign
subset(kosoyCorrected,rowSums(sign(kosoyCorrected))==ncol(kosoyCorrected))
giving
BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894
How to drop all rows in pandas dataframe with negative values?
Use select_dtypes
to get only numeric columns, and keep your code.
df[df.select_dtypes(include=[np.number]).ge(0).all(1)]
Example:
df = pd.DataFrame({'col1': [1,2,3,4],
'col2': [-1, -2, 3, 4],
'col3': ['a','b','c','d'],
'col4': [1,2,3,4]})
col1 col2 col3 col4
0 1 -1 a 1
1 2 -2 b 2
2 3 3 c 3
3 4 4 d 4
gives
col1 col2 col3 col4
2 3 3 c 3
3 4 4 d 4
Select groups where all values are positive
Try
library(data.table)
setDT(df1)[, if(!any(Diff < 0)) .SD, by = Number]
# Number A B Diff
#1: 3 2015-06-04 2015-06-07 3
#2: 3 2015-06-04 2015-06-06 2
Or using dplyr
library(dplyr)
df1 %>%
group_by(Number) %>%
filter(all(Diff>=0))
Related Topics
How to Sort a Data Frame by Alphabetic Order of a Character Variable in R
Break Dataframe into Smaller Dataframe'S and Save Them
How to Give Subtitles for Subplot in Plot_Ly Using R
Pass a String as Variable Name in Dplyr::Filter
Combine a List of Data Frames into One Data Frame by Row
Left Align Two Graph Edges (Ggplot)
Controlling Number of Decimal Digits in Print Output in R
Compare Two Data.Frames to Find the Rows in Data.Frame 1 That Are Not Present in Data.Frame 2
Cannot Install R-Forge Package Using Install.Packages
Create Stacked Barplot Where Each Stack Is Scaled to Sum to 100%
How to Change the Default Colors in Plotly Chart
Easier Way to Use Grepl and Ifelse Across Multiple Columns
Installing Rgl on Ubuntu and Mac: X11 Not Found
Add Regression Line Equation and R^2 on Graph
In R, How to Get an Object'S Name After It Is Sent to a Function