user11645903
user11645903

Reputation:

Wants to delete all rows with negative values in df

I have below df with date columns and wants to delete rows where have negative values

date                  B         C         D         E
2019-07-01 00:00   0.400157  0.978738  2.240893  1.867558
2019-07-01 00:10   0.950088 -0.151357 -0.103219  0.410599
2019-07-01 00:20   1.454274  0.761038  0.121675  0.443863
2019-07-01 00:30   1.494079 -0.205158  0.313068 -0.854096

if code one liner solution is then its best,i tried below line but its showing below error

'Invalid comparison between dtype=datetime64[ns] and int'

df = df[(df > 0).all(axis=1)]

Upvotes: 0

Views: 110

Answers (1)

jottbe
jottbe

Reputation: 4521

You can do that as follows:

# create an indexer for the columns (from including column 3 to the end)
# then "or" them using any, so it is True if the row contains
# any negative value
row_indexer= (df.iloc[:, 2:] < 0).any(axis='columns')
# now drop the columns for which the row_indexer is True
df.drop(df[row_indexer].index)

Upvotes: 1

Related Questions