Delete Rows With Negative Values

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



Leave a reply



Submit